MATLAB: Shuffle matrix elements

shuffle matrix

Hey guys, I want to shuffle a 3×3 matrix (which consist elements within 1:9 unrepeated). So that I have written a very strange code.
>>X=perms(1:9);
Execute above line once.
Then execute below line how many shuffled matrices you want.
>>SMx=reshape(X(randi(size(X,1)),:),3,3)
Is there any better way to do this?

Best Answer

SMx = reshape(randperm(9), 3, 3);
If you have Matlab 2011b, use "randperm(9, 9)" instead: It uses the Fisher-Yates-Shuffle, which is much faster. And if you struggle with large arrays, this is even faster: FEX: Shuffle.