function M=permugen(v)
% generate all N! permutations of a vector v=[1:N]
% use recursive call
% (C) 2001-2005 by Yu Hen Hu
% created: 2001
% modified: 2005

v=diag(diag(v))';  % make sure v is a row vector
N=length(v);
if N==1,
   M=v;
elseif N > 1,
   M=[];
   for i=1:N,
       A=permugen(setdiff(v,v(i)));
      M=[M; [v(i)*ones(size(A,1),1) A] ];
   end
end
