How to shuffle two column array?
Show older comments
Hello,
I'm trying to shuffle both the columns and rows of a two column array, but I'm running into a problem with the randomization of the columns. I want to preserve the pairs in the columns.
Here's a shortened version of the way I've written things:
A = nchoosek(1:4,2);
rowRand = randperm(size(A,1));
B = A(rowRand,:);
colRand = randperm(size(A,2));
C = B(:,colRand);
check = B == C;
Two problems.
- When B is randomized, the number in the first column is always smaller than the number in the second column. While that's okay ~50% of the time, that's what I'm trying to change!
- check is often true, which while I understand why it's happening, is problematic (and related to the above bullet).
Is there a way to do this without a loop? If I were to implement a loop, what would be the best way to go? I'm imagining something using a counter from the length of A... but I'm not sure how to randomize successive rows. Luckily, the array isn't very big in the actual program (usually 28x2).
Accepted Answer
More Answers (0)
Categories
Find more on Special Functions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!