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.
%
%   This function is only practical for situations where N is less
%   than about 15.
%
%   See also NCHOOSEK, RANDPERM, PERMUTE.

%   Copyright (c) 1984-98 by The MathWorks, Inc.
%   $Revision: 1.6 $  $Date: 1997/11/21 23:45:45 $

v = v(:).'; % Make sure v is a row vector
n = length(v);
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
