How to suffle rows of specific rows from a matrix

11 views (last 30 days)
Hello I have a matrix with 2 columns and 120 rows. the first columns has numbers 2-6 and the second one is 0 and 1 like the following
[2 1]
[3 0]
[2 0]
[4 0]
[6 1]
[2 1] etc.
I want to shuffle randomly the second column to produce a new column. In this new column I want to shuffle the rows of the second column, that their first column is between 2 and 5. Also shuffle the rows on the second column that the first column is 6 between them. More specifically if I have a sequence in the following way
[2 1] [3 0] [6 0] [2 0]
one new shuffled column will make the matrix
[2 0 0] [3 0 1] [6 0 0] [2 0 0]
but NOT
[2 0 0] [3 0 0] [6 0 1] [2 0 0]
because i don't wont to shuffle the rows that have 6 with the rows that have 2-5. I want with this process to create 100 new columns and to do this 2008 times with different matrixes of the same size. All this shuffling has to be in a random way except for the condition of 2-5 and 6 that I mentioned.

the cyclist on 1 Jun 2018
Edited: the cyclist on 1 Jun 2018
I think this does what you want. I wrote it from more of a didactic than efficiency approach. I imagine there are some efficiencies here, but one should generally get a working solution first, then consider optimization.
NCOL = 100;
M = [2 1;
3 0;
2 0;
4 0;
6 1;
2 1;
6 0];
M_new = [M, zeros(size(M,1),NCOL)];
isRowSix = M(:,1)==6;
isRowNot = M(:,1)~=6;
M2Six = M(isRowSix,2);
M2Not = M(isRowNot,2);
numberRowsSix = sum(isRowSix);
numberRowsNot = sum(isRowNot);
for nc = 1:NCOL
newOrderSix = randperm(numberRowsSix);
newOrderNot = randperm(numberRowsNot);
newColSix = M2Six(newOrderSix);
newColNot = M2Not(newOrderNot);
M_new(isRowSix,nc+2) = newColSix;
M_new(isRowNot,nc+2) = newColNot;
end
Georgios Tertikas on 5 Jun 2018
thank you very much