Random numbers and storing from 1 -5

6 views (last 30 days)
Hi, Trying to create a random number gen from 1-5 and store into an array. I want the random numbers to generate 600 times giving me an 100 random 1's,2's,3's,4's and 5's. I keep getting size issues of left and right, anyone able to help? I should end up with 600 random distributions of 1-5 but it stops on the first loop with "Unable to perform assignment because the indices on the left side are not compatible with the size of the right side."
Thanks in advance
x=[];
s = 600;
y = [];
for i=1:1:s
y(i)=randperm(5);
y(i) = transpose(y(i));
x = vertcat(x,y(i));
end

Accepted Answer

the cyclist
the cyclist on 7 Feb 2021
I believe this does what you want:
s = 600;
x = zeros(5,s);
for i=1:1:s
x(:,i)=randperm(5)';
end
  9 Comments
the cyclist
the cyclist on 7 Feb 2021
OK. I think I would do the second randomization method as follows:
y = repmat(1:5,1,100);
y = y(randperm(500))
y = 1×500
1 5 2 5 4 2 1 1 3 1 5 5 3 3 5 4 5 4 1 4 5 3 4 5 5 2 2 4 3 3
If you prefer to do the third randomization method, a straightforward approach would be
y = randi(5,1,500)
y = 1×500
2 3 5 4 4 2 4 3 5 3 3 1 3 5 1 3 5 5 3 5 2 1 5 4 3 5 1 5 5 3
BionicP
BionicP on 7 Feb 2021
haha i cant believe it looks that simple XD! Thank you so much this is exactly it!!!!

Sign in to comment.

More Answers (1)

David Hill
David Hill on 7 Feb 2021
y=zeros(5,600);
for k=1:600
y(:,k)=randperm(5);
end
  1 Comment
BionicP
BionicP on 7 Feb 2021
This solution also was perfect, thank you both !!

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!