Count the number of heads per set in Mathematica

主宰稳场 提交于 2019-12-08 04:03:35

问题


First of all, thank you for all your responces. I am doing my best to keep up with all the suggestions. However I'm trying to stay on track.

So now I have

s = Table[RandomChoice[{Heads, Tails}, 2 i + 1], {i, 10}];

Next, I want to count the number of "Heads" that occur for each "i".

I can do this for one case....say 5,

n = RandomChoice[{Heads, Tails}, 5];
n1 = Count[n, Heads];

But I am having trouble expanding this....

Thanks again.

p.s. what are down votes? Too easy?


回答1:


Map (/@) the function of counting heads in a list:

Count[#, Heads]&

to each sublist in s. Hence:

Count[#, Heads]& /@ s



回答2:


If you are doing a simulation of coin flips, I believe that BinomialDistribution is what you want.

Histogram[
  Count[#, "Heads"] & /@
    Table[RandomChoice[{"Heads", "Tails"}, 9], {25000}]
]

BarChart@Table[PDF[BinomialDistribution[9, 1/2], k], {k, 0, 9}]

By the way, the symbol Heads is a built-in function and should probably not be used the way you are using it.



来源:https://stackoverflow.com/questions/5524293/count-the-number-of-heads-per-set-in-mathematica

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!