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 view (last 30 days)
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.

Answers (1)

Steven Lord
Steven Lord on 30 Nov 2020
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.
  2 Comments
Marina Suá
Marina Suá on 30 Nov 2020
But I need to count the number of ones I get, the number of twos I get and so on... I don't know if I'm explaining my self. I don't know if there is a function frorm matlab or not.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!