Mercurial > hg > octave-lyh
annotate scripts/specfun/perms.m @ 9245:16f53d29049f
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 22 May 2009 10:46:00 -0400 |
parents | 1bf0ce0930be |
children | 804c21f3659b |
rev | line source |
---|---|
9245 | 1 ## Copyright (C) 2001, 2006, 2007, 2009 Paul Kienzle |
5827 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
5827 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
5827 | 18 |
19 ## -*- texinfo -*- | |
20 ## @deftypefn {Function File} {} perms (@var{v}) | |
21 ## | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
7017
diff
changeset
|
22 ## Generate all permutations of @var{v}, one row per permutation. The |
5827 | 23 ## result has size @code{factorial (@var{n}) * @var{n}}, where @var{n} |
24 ## is the length of @var{v}. | |
25 ## | |
6754 | 26 ## As an example, @code{perms([1, 2, 3])} returns the matrix |
27 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
7017
diff
changeset
|
28 ## @group |
6754 | 29 ## 1 2 3 |
30 ## 2 1 3 | |
31 ## 1 3 2 | |
32 ## 2 3 1 | |
33 ## 3 1 2 | |
34 ## 3 2 1 | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
7017
diff
changeset
|
35 ## @end group |
6754 | 36 ## @end example |
5827 | 37 ## @end deftypefn |
38 | |
39 function A = perms (v) | |
6391 | 40 if (nargin != 1) |
41 print_usage (); | |
42 endif | |
5827 | 43 v = v(:); |
44 n = length (v); | |
45 if (n == 1) | |
46 A = v; | |
47 else | |
48 B = perms (v(1:n-1)); | |
49 Bidx = 1:size (B, 1); | |
50 A = v(n) * ones (prod (2:n), n); | |
51 A(Bidx,1:n-1) = B; | |
52 k = size (B, 1); | |
53 for i = n-1:-1:2 | |
54 A(k+Bidx,1:i-1) = B(Bidx,1:i-1); | |
55 A(k+Bidx,i+1:n) = B(Bidx,i:n-1); | |
56 k = k + size (B, 1); | |
57 endfor | |
58 A(k+Bidx,2:n) = B; | |
59 endif | |
60 endfunction |