How can I randomly permute row keeping those with the same value adjacent to each other? I know that I can use randperm on the rows to randomly permute all rows, but I do not know how to keep the same-valued rows adjacent. Thanks.
A = [0 0 0;
0 0 0;
1 1 1;
1 1 1;
1 1 1;
2 2 2;
2 2 2];
permute_A = [0 0 0;
0 0 0;
2 2 2;
2 2 2;
1 1 1;
1 1 1;
1 1 1];
You have to identify the clusters and then permute them:
Finding the clusters can be done by finding the changes in their differences:
Then the cluster ends are easily found by:
now you know the number of clusters and you can permute them:
and put them in a new matrix: