% permutations(v) - generate permutation matrix of row vector elements
%
function c = permutations( v)

m = size(v,2);	% columns(v)

if( m == 1)	% 1 permutation is just v
  c = v;
  return;
end

R2 = prod(1:m-1);
R = m * R2;	% number of permutations

c = zeros( m, R);
vnext = v;
i = 1;

while( i <= R)
  cnext = permutations( vnext(2:m));
  c(:,i:i+R2-1) = [vnext(1)*ones(1,R2); cnext];
  vnext = [vnext(2:m) vnext(1)];
  i = i + R2;
end
