Pseudo Random Integer with frequency constraints
Show older comments
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 Comment
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?
Accepted Answer
More Answers (0)
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!