How do I repetitively shuffle elements in my matrix column-wise?

17 views (last 30 days)
Hello everyone,
I am new to MATLAB and have a question about how to randomly shuffle elements in a matrix column-wise. Specifically, I have a 582x255 matrix and I would like to shuffle the elements. However, it is important that the elements of one column stay in the same column, so a column-wise shuffle and that the every column is shuffled in a different way (so that the elements in one row are not all shuffled to the same row). So far, this is what I have come up with:
[m,n]=size(F); % F = 582x255 matrix
nb_rows = n; %number of rows
C_1 = F(:,1); % select first column of matrix
C_1_s = C_1(randperm(length(C_1))); % randomly shuffle the elements of that column
This works. However, I would like to find a way to do it automatically for every column. I think I would need to create a loop that does the same thing column per column, so I have tried this:
for i=1:nb_rows;
C_i = F(i,1);
C_i_s = C_i(randperm(length(C_i)));
end
But it doesn't seem to work.
I would greatly appreciate it if someone could help me with this!

Accepted Answer

Matt J
Matt J on 8 Jan 2024
Edited: Matt J on 8 Jan 2024
F=magic(5)
F = 5×5
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
[m,n]=size(F);
[~,I]=sort(rand([m,n]));
J=repmat(1:n,m,1);
Fshuffle = F(sub2ind([m,n],I,J))
Fshuffle = 5×5
17 6 7 2 3 11 5 25 14 16 4 24 1 21 9 10 18 13 20 22 23 12 19 8 15
  6 Comments
Joyce Oerlemans
Joyce Oerlemans on 10 Jan 2024
That works perfectly, thank you very much! I have one last question: is there a way to repeat this loop a 100 times so that I get 100 variables each which a different shuffle of the matrix?
Matt J
Matt J on 10 Jan 2024
Edited: Matt J on 10 Jan 2024
That works perfectly, thank you very much!
You're welcome, but please Accept-click the answer to indicate that it worked.
I have one last question: is there a way to repeat this loop a 100 times so that I get 100 variables each which a different shuffle of the matrix?
As below:
F=magic(5)
F = 5×5
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
[m,n]=size(F);
p=100;
[~,I]=sort(rand([m,n,p]));
J=repmat(1:n,m,1,p);
Fshuffle = F(sub2ind([m,n],I,J))
Fshuffle =
Fshuffle(:,:,1) = 17 12 1 21 22 10 18 19 20 3 4 6 7 2 15 11 24 13 8 9 23 5 25 14 16 Fshuffle(:,:,2) = 11 24 25 2 22 23 6 7 21 3 4 12 1 8 9 10 5 13 20 16 17 18 19 14 15 Fshuffle(:,:,3) = 4 5 13 21 16 17 18 1 14 22 10 12 19 20 15 23 6 7 8 9 11 24 25 2 3 Fshuffle(:,:,4) = 10 12 1 21 16 17 18 19 20 3 23 6 7 14 15 4 24 13 2 22 11 5 25 8 9 Fshuffle(:,:,5) = 4 12 13 14 16 10 5 25 20 15 11 24 7 8 22 17 6 1 21 9 23 18 19 2 3 Fshuffle(:,:,6) = 11 6 7 14 9 23 18 25 20 3 10 24 13 2 16 17 5 19 8 22 4 12 1 21 15 Fshuffle(:,:,7) = 23 18 19 20 3 10 24 7 21 16 4 12 13 14 15 17 6 25 2 22 11 5 1 8 9 Fshuffle(:,:,8) = 4 18 13 21 16 10 5 7 2 15 23 12 19 20 9 17 24 1 14 22 11 6 25 8 3 Fshuffle(:,:,9) = 11 6 25 20 15 17 5 13 14 9 4 12 7 8 3 10 24 1 21 16 23 18 19 2 22 Fshuffle(:,:,10) = 11 5 13 14 15 17 12 7 21 9 4 6 25 8 3 23 24 1 20 16 10 18 19 2 22 Fshuffle(:,:,11) = 23 24 13 20 9 17 6 1 8 3 4 5 7 21 15 10 18 25 2 22 11 12 19 14 16 Fshuffle(:,:,12) = 10 24 13 21 22 4 6 19 8 3 11 5 7 2 16 23 18 1 14 9 17 12 25 20 15 Fshuffle(:,:,13) = 10 5 13 14 15 4 12 25 21 22 11 24 19 8 9 23 6 7 2 16 17 18 1 20 3 Fshuffle(:,:,14) = 17 5 19 8 16 10 18 25 2 22 4 24 13 20 3 23 12 1 21 9 11 6 7 14 15 Fshuffle(:,:,15) = 17 5 19 21 9 23 12 25 20 3 10 6 13 14 15 4 24 7 8 22 11 18 1 2 16 Fshuffle(:,:,16) = 10 12 7 2 15 23 18 19 8 3 11 24 25 21 9 17 6 1 20 16 4 5 13 14 22 Fshuffle(:,:,17) = 17 12 13 8 16 10 18 7 21 9 23 6 1 20 3 4 24 25 2 15 11 5 19 14 22 Fshuffle(:,:,18) = 17 18 1 2 3 4 12 25 14 16 23 6 19 8 22 11 5 7 20 9 10 24 13 21 15 Fshuffle(:,:,19) = 10 18 19 8 15 17 24 7 20 3 4 5 1 21 9 23 6 13 2 16 11 12 25 14 22 Fshuffle(:,:,20) = 23 5 1 20 9 4 6 25 2 3 17 24 7 8 16 11 12 19 14 22 10 18 13 21 15 Fshuffle(:,:,21) = 4 12 7 21 16 17 6 19 2 9 23 5 1 20 15 10 24 13 8 22 11 18 25 14 3 Fshuffle(:,:,22) = 11 6 19 2 15 23 12 25 21 16 4 5 7 8 3 10 18 13 14 9 17 24 1 20 22 Fshuffle(:,:,23) = 23 18 25 20 16 4 5 19 14 15 11 6 13 8 9 10 12 1 2 22 17 24 7 21 3 Fshuffle(:,:,24) = 4 6 19 8 16 10 5 25 2 9 17 18 7 20 22 11 12 13 14 15 23 24 1 21 3 Fshuffle(:,:,25) = 10 12 1 20 3 4 5 13 2 15 23 18 7 14 9 17 24 25 8 16 11 6 19 21 22 Fshuffle(:,:,26) = 11 24 13 8 3 23 6 1 14 22 10 12 25 2 9 4 5 19 21 16 17 18 7 20 15 Fshuffle(:,:,27) = 10 24 19 20 16 23 18 25 21 15 11 5 7 8 22 17 6 1 14 3 4 12 13 2 9 Fshuffle(:,:,28) = 10 12 13 21 16 23 18 19 8 3 17 6 1 2 15 4 5 25 14 9 11 24 7 20 22 Fshuffle(:,:,29) = 17 18 25 20 22 10 6 1 14 16 23 5 19 8 9 11 12 13 2 15 4 24 7 21 3 Fshuffle(:,:,30) = 11 5 13 8 16 23 18 19 2 3 10 12 1 21 9 4 24 7 14 15 17 6 25 20 22 Fshuffle(:,:,31) = 10 24 25 8 15 17 6 13 14 16 4 5 7 21 22 11 12 1 2 3 23 18 19 20 9 Fshuffle(:,:,32) = 17 6 25 8 22 23 24 1 21 16 11 5 7 2 3 4 18 13 20 15 10 12 19 14 9 Fshuffle(:,:,33) = 4 24 7 2 9 11 18 13 8 16 10 12 25 20 3 23 6 1 14 15 17 5 19 21 22 Fshuffle(:,:,34) = 23 12 1 2 16 10 24 13 14 15 4 6 19 20 9 11 18 7 21 22 17 5 25 8 3 Fshuffle(:,:,35) = 11 6 1 21 22 4 5 7 20 9 10 18 13 2 15 23 24 25 14 3 17 12 19 8 16 Fshuffle(:,:,36) = 4 12 7 8 15 23 6 13 20 9 17 24 19 21 3 11 18 25 14 16 10 5 1 2 22 Fshuffle(:,:,37) = 10 12 7 8 22 4 5 13 21 9 17 18 19 2 15 23 6 1 20 16 11 24 25 14 3 Fshuffle(:,:,38) = 17 18 1 21 9 10 5 13 8 22 4 24 25 14 3 23 12 19 2 16 11 6 7 20 15 Fshuffle(:,:,39) = 10 6 13 8 16 4 18 1 20 9 17 12 19 21 22 23 5 7 14 15 11 24 25 2 3 Fshuffle(:,:,40) = 23 5 1 21 15 10 6 19 8 9 11 18 7 14 16 17 12 25 2 22 4 24 13 20 3 Fshuffle(:,:,41) = 4 6 1 8 22 10 18 13 20 15 11 24 19 2 3 23 5 7 21 9 17 12 25 14 16 Fshuffle(:,:,42) = 4 24 1 8 3 10 12 19 21 22 17 18 25 2 9 23 6 7 20 15 11 5 13 14 16 Fshuffle(:,:,43) = 4 6 13 14 9 17 12 19 20 3 23 18 1 2 22 11 5 7 21 16 10 24 25 8 15 Fshuffle(:,:,44) = 4 12 1 8 22 23 24 7 2 9 17 5 13 14 16 10 6 19 21 3 11 18 25 20 15 Fshuffle(:,:,45) = 23 6 25 2 3 17 18 19 20 16 11 24 1 21 22 4 12 7 8 15 10 5 13 14 9 Fshuffle(:,:,46) = 11 5 7 20 3 17 24 19 8 9 10 6 13 2 16 23 18 1 21 22 4 12 25 14 15 Fshuffle(:,:,47) = 23 12 1 2 3 10 6 19 21 15 11 24 25 8 9 17 5 13 20 16 4 18 7 14 22 Fshuffle(:,:,48) = 23 12 7 20 9 10 6 19 21 16 17 24 25 2 22 11 5 13 14 3 4 18 1 8 15 Fshuffle(:,:,49) = 4 5 19 8 22 11 18 25 21 15 23 24 13 2 3 10 12 1 20 16 17 6 7 14 9 Fshuffle(:,:,50) = 11 6 7 20 16 4 12 19 2 9 10 18 25 14 3 17 24 13 21 15 23 5 1 8 22 Fshuffle(:,:,51) = 11 5 7 8 16 4 24 19 21 22 23 18 13 14 3 10 6 1 2 9 17 12 25 20 15 Fshuffle(:,:,52) = 11 5 7 2 15 4 12 1 14 9 17 6 25 8 16 10 18 13 21 22 23 24 19 20 3 Fshuffle(:,:,53) = 23 12 25 21 22 4 5 19 8 16 11 18 1 14 15 17 6 13 2 9 10 24 7 20 3 Fshuffle(:,:,54) = 10 6 7 21 3 4 5 19 20 15 17 24 25 8 16 23 12 1 2 9 11 18 13 14 22 Fshuffle(:,:,55) = 17 18 19 14 3 4 24 13 2 15 10 6 25 21 16 23 12 1 8 22 11 5 7 20 9 Fshuffle(:,:,56) = 23 5 19 14 3 10 6 1 21 16 11 12 25 2 15 4 24 13 8 9 17 18 7 20 22 Fshuffle(:,:,57) = 17 24 13 20 9 11 5 7 21 16 4 18 25 2 15 23 12 1 14 22 10 6 19 8 3 Fshuffle(:,:,58) = 10 12 25 2 3 17 6 19 21 22 11 24 1 8 9 23 5 13 14 16 4 18 7 20 15 Fshuffle(:,:,59) = 4 5 25 8 22 17 6 7 21 3 11 24 1 20 15 10 18 13 2 9 23 12 19 14 16 Fshuffle(:,:,60) = 4 18 13 20 16 23 5 19 21 15 11 12 25 8 9 17 6 7 2 3 10 24 1 14 22 Fshuffle(:,:,61) = 10 18 13 2 9 23 5 1 21 22 17 12 7 8 15 4 6 19 14 3 11 24 25 20 16 Fshuffle(:,:,62) = 17 24 1 2 22 4 12 25 14 3 23 6 7 20 15 10 18 13 8 9 11 5 19 21 16 Fshuffle(:,:,63) = 10 12 25 20 15 11 24 13 8 16 4 5 1 2 22 23 6 19 21 9 17 18 7 14 3 Fshuffle(:,:,64) = 17 18 19 2 9 11 12 25 21 3 4 5 13 14 16 23 6 1 20 15 10 24 7 8 22 Fshuffle(:,:,65) = 10 18 7 8 16 23 5 1 20 22 11 6 25 14 15 4 24 13 2 9 17 12 19 21 3 Fshuffle(:,:,66) = 4 24 1 21 9 10 5 13 8 15 17 6 25 20 3 11 18 7 14 16 23 12 19 2 22 Fshuffle(:,:,67) = 23 5 13 21 16 4 18 19 20 22 17 12 1 8 15 10 6 25 14 9 11 24 7 2 3 Fshuffle(:,:,68) = 11 18 19 20 22 23 5 13 14 9 4 24 25 8 3 10 12 7 21 15 17 6 1 2 16 Fshuffle(:,:,69) = 11 18 7 21 15 17 5 19 2 22 4 12 25 20 16 23 6 13 8 9 10 24 1 14 3 Fshuffle(:,:,70) = 10 5 13 2 9 23 24 25 20 22 17 18 1 14 15 4 12 7 21 3 11 6 19 8 16 Fshuffle(:,:,71) = 11 12 25 21 9 17 6 19 14 15 4 18 13 8 22 23 24 7 2 3 10 5 1 20 16 Fshuffle(:,:,72) = 23 12 7 14 16 11 18 25 2 15 10 6 19 8 22 4 24 1 20 3 17 5 13 21 9 Fshuffle(:,:,73) = 23 6 7 20 16 4 18 25 2 3 11 12 19 14 22 10 24 13 21 9 17 5 1 8 15 Fshuffle(:,:,74) = 11 12 1 2 16 23 24 19 21 15 4 5 25 20 3 17 6 7 14 22 10 18 13 8 9 Fshuffle(:,:,75) = 11 6 1 20 22 10 24 25 21 15 23 18 19 2 9 4 12 13 14 3 17 5 7 8 16 Fshuffle(:,:,76) = 23 5 1 20 9 17 6 7 2 3 10 24 25 21 22 4 18 19 14 15 11 12 13 8 16 Fshuffle(:,:,77) = 4 18 25 2 3 11 5 13 21 16 10 12 7 8 15 17 6 19 14 9 23 24 1 20 22 Fshuffle(:,:,78) = 10 18 19 2 3 11 5 13 21 22 4 12 1 20 15 17 6 25 8 16 23 24 7 14 9 Fshuffle(:,:,79) = 10 6 25 20 3 11 5 7 14 15 23 18 13 8 22 4 24 1 2 16 17 12 19 21 9 Fshuffle(:,:,80) = 11 12 19 2 15 10 24 1 21 3 17 6 7 8 9 4 5 13 20 22 23 18 25 14 16 Fshuffle(:,:,81) = 11 5 13 2 9 17 24 25 8 15 10 12 7 20 3 4 18 19 14 22 23 6 1 21 16 Fshuffle(:,:,82) = 4 24 1 14 9 11 6 13 21 16 23 12 19 8 3 17 5 25 2 22 10 18 7 20 15 Fshuffle(:,:,83) = 17 18 25 20 22 11 6 7 8 15 23 5 19 2 9 10 24 13 14 3 4 12 1 21 16 Fshuffle(:,:,84) = 11 12 19 14 15 10 18 7 21 22 17 24 25 8 16 23 5 13 2 3 4 6 1 20 9 Fshuffle(:,:,85) = 10 24 13 21 3 17 5 7 20 22 4 12 1 2 15 11 6 19 8 16 23 18 25 14 9 Fshuffle(:,:,86) = 11 6 19 8 9 4 24 25 14 3 10 5 13 2 15 23 12 7 20 22 17 18 1 21 16 Fshuffle(:,:,87) = 11 5 13 20 16 23 12 1 2 15 10 18 7 21 3 4 24 25 8 9 17 6 19 14 22 Fshuffle(:,:,88) = 23 12 13 8 9 4 6 1 14 15 10 24 7 2 16 17 18 25 20 3 11 5 19 21 22 Fshuffle(:,:,89) = 4 24 19 20 9 23 5 1 21 22 11 6 7 14 3 10 12 25 2 15 17 18 13 8 16 Fshuffle(:,:,90) = 11 12 19 2 9 10 24 1 8 3 17 5 13 14 16 23 6 7 20 22 4 18 25 21 15 Fshuffle(:,:,91) = 17 6 19 2 9 10 12 25 21 22 23 18 1 14 3 11 24 13 20 15 4 5 7 8 16 Fshuffle(:,:,92) = 10 24 7 8 3 23 12 25 20 15 11 6 1 21 22 17 18 13 2 16 4 5 19 14 9 Fshuffle(:,:,93) = 23 5 19 14 9 17 24 13 8 16 4 6 7 20 15 10 12 25 21 22 11 18 1 2 3 Fshuffle(:,:,94) = 17 5 7 20 15 10 18 13 8 16 23 6 25 14 9 11 24 1 21 3 4 12 19 2 22 Fshuffle(:,:,95) = 11 12 19 21 3 17 5 7 8 16 23 6 1 20 15 10 18 13 2 22 4 24 25 14 9 Fshuffle(:,:,96) = 10 18 1 20 3 23 5 19 14 22 11 6 25 2 15 17 24 13 21 9 4 12 7 8 16 Fshuffle(:,:,97) = 4 6 13 14 16 23 5 19 20 3 10 18 7 2 9 17 12 25 8 15 11 24 1 21 22 Fshuffle(:,:,98) = 11 5 7 8 15 23 24 19 21 3 10 6 1 2 9 4 12 25 14 16 17 18 13 20 22 Fshuffle(:,:,99) = 17 24 13 20 9 11 12 7 14 3 10 5 19 2 16 4 18 1 8 22 23 6 25 21 15 Fshuffle(:,:,100) = 4 18 1 20 22 23 24 13 8 16 17 12 7 2 3 10 6 19 14 15 11 5 25 21 9

Sign in to comment.

More Answers (1)

Voss
Voss on 8 Jan 2024
Looks like this is what you are going for:
[m,n]=size(F); % F = 582x255 matrix
for i = 1:n
C_i = F(:,i);
C_i_s = C_i(randperm(m));
F(:,i) = C_i_s;
end
And it can be written more succinctly wihout the temporary variables:
[m,n]=size(F); % F = 582x255 matrix
for i = 1:n
F(:,i) = F(randperm(m),i);
end
  2 Comments
Joyce Oerlemans
Joyce Oerlemans on 9 Jan 2024
Hallo Voss,
thank you very much for your quick response. Could you elaborate a little bit more how I should interpret the output? Is it one possible matrix or does this give all possible matrices at once?
Thank you very much!
Voss
Voss on 9 Jan 2024
It is one matrix. Each column of F is shuffled separately one time and stored back in F.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!