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.
댓글 수: 0
답변 (1개)
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Annotations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!