SHAKE - Randomize a matrix along a specific dimension
Y = SHAKE(X) randomizes the order of the elements in each column of the 2D matrix. For N-D matrices it randomizes along the first dimension.
SHAKE(X,DIM) randomizes along the dimension DIM.
[Y, I, J] = SHAKE(...) return index matrices I and J so that Y = X(I) and X = Y(J).
A = [1 2 3 ; 4 5 6 ; 7 8 9 ; 10 11 12] ; % see <slm> on the FEX ...
Dim = 2 ;
[B, ID] = shake(A,Dim) % ->, e.g.
% 3 2 1
% 6 4 5
% 7 8 9
% 11 10 12%
See also RAND, SORT, RANDPERM
and RANDSWAP on the File Exchange
Latest version: 4.1 (may 2008)
I found I needed to change line 68 when going to higher dimensions. Not thoroughly tested but this may work:
I = shiftdim(reshape(I,sz),dim-ndim);
Thanks, this is just what I needed. However I'm finding it to be quite slow. I have ~10000 rows and 5 columns, and I am randomising by column (Dim = 1). Do you have any suggestions as to how to speed this up? Is doing each column vector separately faster?
New version: 3.0 (oct 2006)
Simple? Yes, but I couldn't figure out how to do it on my own, and it does exactly what it's supposed to do, so thanks, Jos.
Which tests? Note that <shake> simply uses the built-in matlab <rand> function ...
Tests show that the quality of randomization is not good. Cannot be trusted in serious simulations.
Its not shaky at all. Certainly not its documentation, which is as clear as even I could ask for.
dec 2006 - fixed major bug in functionality
SHAKE can now randomize along any dimension.