Random numbers and storing from 1 -5

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

 Risposta accettata

I believe this does what you want:
s = 600;
x = zeros(5,s);
for i=1:1:s
x(:,i)=randperm(5)';
end

9 Commenti

Thank you so much !!
Hi sorry i actually realised why i was concatenating. I am going to use these numbers to label some data that will train a model. Therefore i need the data to follow in one dimension i.e. 1x600 or 600x1. Is there any way I can change the data to do this?
I don't understand what you are asking here, and now I am beginning to question whether I fully understood your original question, because you mention "100 random 1s ... 5s". There is nothing related to 100 in my answer.
Your original code looked like it was just trying to randomly permute the vector [1,2,3,4,5] -- and do that 600 times. This would lead to a 5x600 matrix, in which each column has 1,2,3,4,5 exactly once, just in a different order.
Is that what you want? Where does the 100 come in?
Or did you actually want 600 iterations of a vector that is 100x1, where each entry is randomly selected to be 1, 2, 3, 4, or 5?
Please clarify exactly what the output should be. Maybe provide an example of the output, where instead of 600 iterations, you only need 6?
Ok so what i am looking for is a series of randomly permuted iterations of the vector [1,2,3,4,5] as you have said, and yes again do that 600 times, but i simply wanted each permutation to append onto the next so moving either horizontally or vertically to 600. What you have given me is almost what I needed, but instead of it being a 5x600 i want it to be 1x600 (or 600x1), (First permutation from 1-5, second from 6-10 etc). I hope thats clearer! Thanks in advance!
At the end, the data should show that there are 100 1's, 2's, 3's, 4's and 5's just in a random order essentially! but just either horizontally or vertically
the cyclist
the cyclist il 7 Feb 2021
Modificato: the cyclist il 7 Feb 2021
That's clearer, but I am still a bit confused. How many total numbers do you want to generate?
  1. 120 sets of permuted [1,2,3,4,5] = 600 numbers
  2. 100 sets of permuted [1,2,3,4,5] = 500 numbers
  3. 600 sets of permuted [1,2,3,4,5] = 3,000 numbers
  4. something else?
And how do you want them randomized?
  1. Each group of 5 has to be its own, independent permutation of [1,2,3,4,5]
  2. It's ok if a group of 5 is "unbalanced" (e.g. [1,4,4,3,2]), but over the entire output, each number has to appear an equal number of times.
  3. It doesn't matter how many times each number appears, as long as each number has an equal probability of appearing. So, for example, the set of 600 number might have the followiing counts of [1,2,3,4,5]: 119 1's, 123 2's, 127 3's, 120 4's, and 111 5's. (The different counts are because of random sample variance, just like dice rolls.)
sorry haha 100 sets of permuted [1,2,3,4,5] totalling 500. I confused myself with the 600 as i took some other data i am collecting into consideration.
The second option of randomisation should suffice; for some backstory i want to create a machine learning model that can detect individual finger flexion, so i think giving the same amount of data for each finger in a random order should be appropriate.
Thanks again
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
haha i cant believe it looks that simple XD! Thank you so much this is exactly it!!!!

Accedi per commentare.

Più risposte (1)

y=zeros(5,600);
for k=1:600
y(:,k)=randperm(5);
end

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by