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 日間)
Marina Suá
Marina Suá 2020 年 11 月 30 日
コメント済み: Walter Roberson 2020 年 11 月 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.
Walter Roberson
Walter Roberson 2020 年 11 月 30 日
histcounts is a function to do counting.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by