Hi, My histogram is not showing what I would like can I get some help please?

8 ビュー (過去 30 日間)
Milan Sumegi
Milan Sumegi 2018 年 12 月 30 日
コメント済み: Milan Sumegi 2018 年 12 月 31 日
This is my code is running and making histogram as well, but not at the right place.
A=input('How many tossing would you like to do? ')
x= rand(A,1);
if(x<0.5),
toss = -1; %Head
else
toss = 1; %Tail
end
hist(x,-2:2)
Asyou can se one bar is at 0 and the other is at 1. I would like if Heads been shown at -1 and Tails at 1.
Thank you for any help.

採用された回答

Michael Madelaire
Michael Madelaire 2018 年 12 月 30 日
編集済み: Michael Madelaire 2018 年 12 月 30 日
The problem is that you are not storing the answers/toss.
A=input('How many tossing would you like to do? ')
x= rand(A,1);
toss = nan(A,1);
for i=1:length(x)
if x(i)<0.5
toss(i) = -1; %Head
elseif x(i)>=0.5
toss(i) = 1; %Tail
else
disp('Error...')
end
end
histogram(toss)
Edit: If you want it more neat
A=input('How many tossing would you like to do? ')
x= rand(A,1);
above = x < 0.5;
x(above)=-1;
x(~above)=1;
histogram(toss)
  3 件のコメント
Image Analyst
Image Analyst 2018 年 12 月 31 日
Or even more compact (though perhaps a little harder to understand):
numTosses = 100000
tosses = 2 * (rand(1, numTosses) < 0.5) - 1;
histogram(tosses)
Milan Sumegi
Milan Sumegi 2018 年 12 月 31 日
Its ok I used the first one. I need write an essay about my codes so I need at least understand a bit. but thanx for the answare.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLive Scripts and Functions についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by