Hello, I would like to alter my code so that every value for position is stored in a matrix rather than dispaying only the final value so I can plot them later. How would I write this out? Here's what I've got. Thanks!
position = 0;
tails = 0;
heads = 1;
for s = (1:1000)
x=randi([0 1]);
if x==tails
position = position-1;
elseif x==heads
position = position+1;
end
end

 채택된 답변

madhan ravi
madhan ravi 2018년 11월 23일

0 개 추천

position = zeros(1,1000);
position(1)=0;
tails = 0;
heads = 1;
for s = 2:1000
x=randi([0 1]);
if x==tails
position(s) = position(s-1)-1;
elseif x==heads
position(s) = position(s-1)+1;
end
end

추가 답변 (1개)

Luna
Luna 2018년 11월 23일

0 개 추천

Hello Estevan,
Try this:
position = zeros(1,1000);
tails = 0;
heads = 1;
for s = (1:1000)
x=randi([0 1]);
if x==tails
position(s) = position(s)-1;
elseif x==heads
position(s) = position(s)+1;
end
end

댓글 수: 2

Estevan Munoz
Estevan Munoz 2018년 11월 23일
Unfortunately doesn't dispaly data correctly. Matlab 1.png
Estevan Munoz
Estevan Munoz 2018년 11월 23일
The orginal data became skewed for some reason. Matlab 2.png

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2018년 11월 23일

댓글:

2018년 11월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by