Shuffle
>Psychtoolbox>PsychProbability
[Y,index] = Shuffle(X [, bindDim])
Randomly sorts X.
If X is a vector, sorts all of X, so Y = X(index).
If X is an m-by-n matrix, sorts each column of X, so
for j=1:n, Y(:,j)=X(index(:,j),j).
The optional ‘bindDim’ parameter allows this function to shuffle with the
same order across the bindDim. It also work with higher dimension arrays.
for example, if you have an n by m matrix X and hope shuffle each column
with same random order (Shuffle the rows), rather than shuffle each
column independently, you can run Shuffle(X, 2). This function also works
on higher dimension arrays. say a 3-d array, If you bind the 2nd
dimension, it will shuffle the rows on each page independently. If I bind
the 2nd and 3rd dimension, then it will shuffle the layer of the 3-d
array.
Examples:
create a 2-d array:
x = repmat((1:4)’,1,5)
Shuffle each column of x independently:
y1 = Shuffle(x)
Shuffle columns of x with same order:
y2 = Shuffle(x,2)
Create a 3-d array (4 by 5 by 3), each column contains a vector [1:4]’
x = reshape(repmat(reshape(kron([1,1,1],1:4),4,3),5,1),4,5,3)
Shuffle each column independently on each page:
[y,ind]=Shuffle(x)
Shuffle 1*5 rows independently on each page:
[y,ind]=Shuffle(x,[2])
Shuffle 1*1*3 rows independently on each column:
[y,ind]=Shuffle(x,[3])
Create a 3-d array (3 by 4 by 5), each row contains a vector [1:4]
x = reshape(repmat(reshape(kron(1:4,[1,1,1]),3,4),1,5),3,4,5)
Shuffle 3*1*5 page along the 2nd dimension:
[y,ind]=Shuffle(x,[1,3])
This is same as:
y2 = RandDim(x,2)
x = reshape(repmat(reshape(repmat(reshape(kron(1:4,[1,1,1]),3,4),1,5),3,4,5),1,2),3,4,5,2)
[y,ind]=Shuffle(x,[1,3])
Also see SORT, Sample, Randi, RandDim, and RandSample.
Psychtoolbox/PsychProbability/Shuffle.m