How to find average?

4 ビュー (過去 30 日間)
Brian Liu
Brian Liu 2018 年 1 月 21 日
回答済み: Image Analyst 2018 年 1 月 21 日
Hi,
I'm a beginner to MATLAB. I'm simulating a coin toss and I'm trying to find the average across the 10 iterations.
for i = 1:10
heads = 0;
numflips = 0;
payoffs = 0;
sumpayoff = 0;
while heads == 0
numflips = numflips + 1;
if rand () > .5
payoff = 2^numflips;
heads = 1;
sumpayoff = sumpayoff + payoff;
else
heads = 0;
end
end
payoff
numflips
avgpayoff = sumpayoff/numflips
end
Could somebody explain why this doesn't work? The "sumpayoff" bit isn't tallying up the payoffs across the 10 iterations accurately.

回答 (1 件)

Image Analyst
Image Analyst 2018 年 1 月 21 日
Maybe say
avgpayoff(i) = sumpayoff/numflips % Get payoff for this iteration.
Then after the loop do
meanPayOff = mean(avgpayoff);

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by