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.

 Respuesta aceptada

KALYAN ACHARJYA
KALYAN ACHARJYA el 24 de Nov. de 2018
Editada: madhan ravi el 24 de Nov. de 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 comentarios

it shows error:
Undefined function 'Right' for input arguments of type 'double'.
Chase Lee
Chase Lee el 24 de Nov. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Versión

R2018a

Etiquetas

Preguntada:

el 24 de Nov. de 2018

Comentada:

el 24 de Nov. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by