How to shuffle two column array?

4 visualizaciones (últimos 30 días)
aburl
aburl el 14 de Sept. de 2018
Comentada: Greg el 15 de Sept. de 2018
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).

Respuesta aceptada

Greg
Greg el 14 de Sept. de 2018
Editada: Greg el 14 de Sept. de 2018
The values of B being in ascending order along a row has nothing to do with the randomization. It is because A is in order - because that is how nchoosek works. Does your actual use case involve the application of nchoosek to generate A? If no, your code should work.
In either case, I suspect you want to randomize the columns on a per-row basis, rather than an array-wide column permutation. For that, a loop would be pretty simple:
[nrow,ncol] = size(A);
C = B;
for irow = 1:nrow
colRand = randperm(ncol);
C(irow,:) = B(irow,colRand);
end
I'm not seeing a way to do it without a loop right off, but one probably exists. Sometimes the non-loop methods aren't worth the hassle - they can be obfuscated or complicated, and occasionally even slower.
  2 comentarios
aburl
aburl el 14 de Sept. de 2018
Editada: aburl el 14 de Sept. de 2018
Thank you! That worked great.
I learned (from this) that nchoosek does work in order, but it serves it's purpose in this application well. (It's randomizing the order of an experiment in which participants compare two versions of a stimulus. I don't want any comparison done more than once. The size of the number is somewhat indicative of the stimulus itself, so while the first randomization (B) worked partially, having a smaller number presented first each time might lead to an order effect down the road. The number of comparisons can vary too, so I made a function that uses nchoosek and then shuffles everything around. I just didn't realize until now that the columns weren't happy).
Thanks again! I'm very new to MATLAB and there might have been an easier way to do this whole dang project... but this solution works for me :)
Greg
Greg el 15 de Sept. de 2018
Happy to help. Can't be an expert on day 1, so start somewhere. The first approach that makes sense and gives acceptable results - that's a great place to start.
On that note, I'm not coming up with any better approach to (my understanding of) your problem. So kudos there!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos


Versión

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by