How many times does each combination of numbers appear?

2 ビュー (過去 30 日間)
Jake Mcgee
Jake Mcgee 2019 年 10 月 21 日
編集済み: per isakson 2019 年 10 月 21 日
Let's say I ran randi(1-40) to produce 5 columns and 100 rows Would I be able to get it to tell me how many times, 1 and 2, for example, come up in the same row. Then if not how may times another combination would come up like 3 and 4 for example? Thanks everyone for any help
  2 件のコメント
Guillaume
Guillaume 2019 年 10 月 21 日
randi is just a function to generate random numbers. The only thing you get out of it is the random numbers. But yes, you can easily write code to get the histogram of the random numbers generated.
Jake Mcgee
Jake Mcgee 2019 年 10 月 21 日
please can you show me an example, i am relativly new to matlab but if you wrote a wuick small example im sure i could figure it out.
Thank you so much for your time

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

採用された回答

per isakson
per isakson 2019 年 10 月 21 日
編集済み: per isakson 2019 年 10 月 21 日
"get it to tell" depends on what the little word "it" refers to. The function randi() cannot tell that.
Try this as a start
%%
M = randi([1,5],100,5);
has_12 = arrayfun( @(jj) all( ismember( [1,2], M(jj,:) ) ), (1:size(M,1)) );
has_34 = arrayfun( @(jj) all( ismember( [3,4], M(jj,:) ) ), (1:size(M,1)) );
has = has_12 & has_34;
>> sum(has)
ans =
10
>>
In this case, ten out of one hundred rows has 1,2,3 and 4,

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by