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

2 件のコメント

Estevan Munoz
Estevan Munoz 2018 年 11 月 23 日
Perfect! Thank you!
madhan ravi
madhan ravi 2018 年 11 月 23 日
Anytime :)

サインインしてコメントする。

その他の回答 (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 についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by