I am having trouble running multiple simulations of a random walk situation. Although I have put it into a for loop, I am having trouble making it work.
n = 10001;
for k = 1:n
result = zeros(n,1);
for z = 2:1001
w = rand(1001,1);
if (w(z) >= .5)
Left(z) = Left(z-1) + 1;
Right(z) = Right(z-1);
else
Right(z) = Right(z-1) + 1;
Left(z) = Left(z-1);
end
end
Y = Left - Right;
end
result(k) = Y;
I previously got help with this not too long ago, and I was told to put the assignment for the result in the outer for loop, but then I started getting different issues with result(k) and Y not being the same size. It would be great if I could get an explanation of how to fix this.

 Réponse acceptée

KALYAN ACHARJYA
KALYAN ACHARJYA le 24 Nov 2018
Modifié(e) : madhan ravi le 24 Nov 2018
I have read your previous question, there @Walter sir suggested to keep the result within the outer loop, as follows, still if you face the issue, let me know
n = 10001;
for k = 1:n
result = zeros(n,1);
for z = 2:1001
w = rand(1001,1);
if (w(z) >= .5)
Left(z) = Left(z-1) + 1;
Right(z) = Right(z-1);
else
Right(z) = Right(z-1) + 1;
Left(z) = Left(z-1);
end
end
Y = Left - Right;
result(k) = Y;
end

2 commentaires

it shows error:
Undefined function 'Right' for input arguments of type 'double'.
Chase Lee
Chase Lee le 24 Nov 2018
Yeah I tried the fix that you suggested, but now I get another error from the result(k) = Y line that says "Unable to perform assignment because the left and right sides have a different number of elements." Any explanation would be appreciated.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Produits

Version

R2018a

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by