How do i randomly distribute 4 numbers in 1*100 matrix?
2 views (last 30 days)
Show older comments
Rakibul Islam Rony
on 12 Feb 2018
Commented: Rakibul Islam Rony
on 12 Feb 2018
Hi Helpful minds,
I have 4 numbers, [2, 4, 7, 9] I want to generate a 1*200 Matrix which have only these 4 numbers randomly distributed. I am expecting a matrix like: [7, 9, 4, 4, 2, 9, 7, 4, 2, .....]
Thank you in advance Rony
0 Comments
Accepted Answer
Matt
on 12 Feb 2018
Hi Rakibul,
What you can do is create a matrix containing the numbers you want. I will call it L. Then create a random integer matrix that goes from 1 to 4. This matrix will then be used to index into L. The matrix M will then have the 4 numbers from L randomly distributed. (See code below)
Matt
L = [2,4,7,9];
ind = randi([1 4],1,200);
M = L(ind)
More Answers (0)
See Also
Categories
Find more on Random Number Generation 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!