Mercurial > hg > octave-lyh
annotate scripts/specfun/perms.m @ 11188:4cb1522e4d0f
Use function handle as input to cellfun,
rather than quoted function name or anonymous function wrapper.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Wed, 03 Nov 2010 17:20:56 -0700 |
parents | 693e22af08ae |
children | fd0a3ac60b0e |
rev | line source |
---|---|
9245 | 1 ## Copyright (C) 2001, 2006, 2007, 2009 Paul Kienzle |
9836 | 2 ## Copyright (C) 2009 VZLU Prague |
5827 | 3 ## |
4 ## This file is part of Octave. | |
5 ## | |
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 | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
5827 | 10 ## |
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. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
5827 | 19 |
20 ## -*- texinfo -*- | |
21 ## @deftypefn {Function File} {} perms (@var{v}) | |
22 ## | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
7017
diff
changeset
|
23 ## Generate all permutations of @var{v}, one row per permutation. The |
5827 | 24 ## result has size @code{factorial (@var{n}) * @var{n}}, where @var{n} |
25 ## is the length of @var{v}. | |
26 ## | |
6754 | 27 ## As an example, @code{perms([1, 2, 3])} returns the matrix |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
9836
diff
changeset
|
28 ## |
6754 | 29 ## @example |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
7017
diff
changeset
|
30 ## @group |
6754 | 31 ## 1 2 3 |
32 ## 2 1 3 | |
33 ## 1 3 2 | |
34 ## 2 3 1 | |
35 ## 3 1 2 | |
36 ## 3 2 1 | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
7017
diff
changeset
|
37 ## @end group |
6754 | 38 ## @end example |
5827 | 39 ## @end deftypefn |
40 | |
41 function A = perms (v) | |
6391 | 42 if (nargin != 1) |
43 print_usage (); | |
44 endif | |
5827 | 45 v = v(:); |
46 n = length (v); | |
9836 | 47 |
48 if (n == 0) | |
49 A = []; | |
5827 | 50 else |
9836 | 51 A = v(1); |
52 for j = 2:n | |
53 B = A; | |
54 A = zeros (prod (2:j), n, class (v)); | |
55 k = size (B, 1); | |
56 idx = 1:k; | |
57 for i = j:-1:1 | |
58 A(idx,1:i-1) = B(:,1:i-1); | |
59 A(idx,i) = v(j); | |
60 A(idx,i+1:j) = B(:,i:j-1); | |
61 idx += k; | |
62 endfor | |
5827 | 63 endfor |
64 endif | |
65 endfunction |