function P = perms(v)
%PERMS  All possible permutations.
%   PERMS(1:n), or PERMS(v) where v is a vector of length n, creates
%   a matrix with n! rows and n columns containing all possible
%   permutations of the n elements.  Warning: n > 6 or 7 is impractical.

%   C. Moler 8-1-95
%   Copyright (c) 1984-96 by The MathWorks, Inc.
%   $Revision: 2.4 $  $Date: 1996/03/05 15:08:03 $

[m, n] = size(v);
if n == 1
   n = m;
   v = v';
end

if n == 1
   P = v;
else
   P = [];
   for k = 1:n
      w = v;
      w(k) = [];
      Q = perms(w);
      P = [P; [Q v(ones(size(Q,1),1),k)]];
   end
end
