MATLAB: Initializing Matrix for all possible sequences of numbers

matrix manipulation

Say n=3, I want to initialize this vector…
[1 2 3; 1 3 2; 2 1 3; 2 3 1; 3 1 2; 3 2 1]
That is, all possible sequences for 1, 2 and 3.
n = 3
np = factorial(n); % Number of possible combinations
m = zeros(np,n); % Allocate matrix
for i = 1:np
for j = 1:szp
m(i,j) = 1
end
end
So, this is how far I am now. This writes 1 into m(1,1) then m(1,2)……m(6,3).
I can't think of a way using a loop to change the matrix to how I want to.
Any help is much appreciated.

Best Answer

doc perms
perms(1:3)