MATLAB: Random presentation of the columns of a matrix

random shuffle columns

Hello everyone! I am really new on matlab and I need to know how I can shuffle the columns of a matrix without changing the order of elements in every row. My code is this:
trialType = [1 2 3];
Examples = [4 5 6];
% Make a condition matrix
a = [trialType(1) Examples(1)]; %zeugos x & 1 fora
b = [trialType(1) Examples(2)]; %zeugos x & 2 fores
c = [trialType(1) Examples(3)]; %zeugos x & 3 fores
d = [trialType(2) Examples(1)]; %zeugos y & 1 fora
e = [trialType(2) Examples(2)]; %zeugos y & 2 fores
f = [trialType(2) Examples(3)]; %zeugos y & 3 fores
g = [trialType(3) Examples(1)]; %zeugos both & 1 fora
h = [trialType(3) Examples(2)]; %zeugos both & 2 fores
i = [trialType(3) Examples(3)]; %zeugos both & 3 fores
condMat = [a; b; c; d; e; f; g; h; i];
So disp(condMat) will give me:
1 4
1 5
1 6
2 4
2 5
2 6
3 4
3 5
I want something like this:
1 6
3 4
1 4
etc.
Thanks on advance for your replies 🙂

Best Answer

First, a simpler way to generate your matrix:
trialType = [1 2 3];
Examples = [4 5 6];
[c1, c2] = ndgrid(trialType, Examples);
condMat = [c1(:), c2(:)];
Then, to randomly permute the rows of condMat, use randperm to generate a randomly ordered list of row indices:
condMat = condMat(randperm(size(condMat, 1)), :)