multiple simulation runs for random walk

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.

 采纳的回答

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 个评论

it shows error:
Undefined function 'Right' for input arguments of type 'double'.
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.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

版本

R2018a

标签

Community Treasure Hunt

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

Start Hunting!

Translated by