Imagine a dice tetra-brick (four faces: 1, 2, 3, 4) with the following probabilities [0.5, 0.2, 0.2, 0.1] . Let Y be the Random Variable that simulate the output of rolling the dice.

1 次查看(过去 30 天)
I'm trying to make an histogram from this function but I don't know how to count the number of outcomes, meaning how many 1's I got, the number of 2's and so on.
My code looks like this: disp(["The value that came out is:",num2str(randsample((1:4),100,true,[0.5,0.2,0.2,0.1]))])
And it returns a 1,2,3 or 4, in a 100 examples, so I want to count the number of ones like I said.

回答(1 个)

Steven Lord
Steven Lord 2020-11-30
Use histcounts or histogram.
Alternately if this is part of a homework assignment and you're instructed not to use those functions, a for loop where you add 1 to elements of a vector would work too.
You should assign the output of randsample to a variable so you can reuse it both in your display code as well as the counting code.

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by