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.
  댓글 수: 2
Marina Suá
Marina Suá 2020년 11월 30일
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.

댓글을 달려면 로그인하십시오.

카테고리

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