Pseudo Random Integer with frequency constraints
古いコメントを表示
Hello,
I am relatively new to MATLAB and I need to design a 200x1 matrix, which is filled with random integers that can be either 1/2/3/4, so 4 possible numbers. However, in the matrix I want '1' to occur 70% (thus a total frequency of 140 for number 1), '2', '3' and '4', to occur 10% (thus a frequency of 20 for 2,3 and 4).
Moreover, I want the matrix to be filled so that the values of 2,3, and 4 never display a consecutive repeat, but that 1 may feature consecutive repeats (as it takes 70%)
I had a solution (without the consecutive repeat constraint), using the repelem function. However, on the target PC, an older version of matlab is installed (2013) and does not include this function. Could someone provide me with a solution?
Thanks
1 件のコメント
Do you just need one solution or to generate a random one? One simple solution is:
n = 200;
n1 = 0.7*n; n2 = 0.1*n;
M = [ones(n1,1) ; repmat(2:4,1,n2)'];
But I guess that's not what you want! What was your solution using repelem?
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!