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.

 채택된 답변

KALYAN ACHARJYA
KALYAN ACHARJYA 2018년 11월 24일
편집: madhan ravi 2018년 11월 24일

0 개 추천

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'.
Chase Lee
Chase Lee 2018년 11월 24일
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

태그

질문:

2018년 11월 24일

댓글:

2018년 11월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by