1 ## Copyright (C) 2001-2012 Paul Kienzle
2 ## Copyright (C) 2009 VZLU Prague
4 ## This file is part of Octave.
6 ## Octave is free software; you can redistribute it and/or modify it
7 ## under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 3 of the License, or (at
9 ## your option) any later version.
11 ## Octave is distributed in the hope that it will be useful, but
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ## General Public License for more details.
16 ## You should have received a copy of the GNU General Public License
17 ## along with Octave; see the file COPYING. If not, see
18 ## <http://www.gnu.org/licenses/>.
21 ## @deftypefn {Function File} {} perms (@var{v})
23 ## Generate all permutations of @var{v}, one row per permutation. The
24 ## result has size @code{factorial (@var{n}) * @var{n}}, where @var{n}
25 ## is the length of @var{v}.
27 ## As an example, @code{perms([1, 2, 3])} returns the matrix
41 function A = perms (v)
54 A = zeros (prod (2:j), n, class (v));
58 A(idx,1:i-1) = B(:,1:i-1);
60 A(idx,i+1:j) = B(:,i:j-1);
70 %!assert (perms ([1,2,3]), [1,2,3;2,1,3;1,3,2;2,3,1;3,1,2;3,2,1]);
71 %!assert (perms (1:3), perms ([1,2,3]));
73 %!assert (perms (int8([1,2,3])), int8([1,2,3;2,1,3;1,3,2;2,3,1;3,1,2;3,2,1]));