Mercurial > hg > octave-nkf
annotate scripts/specfun/perms.m @ 16403:1de4ec2a856d
Matlab compatibility for strsplit()
* scripts/strings/strsplit.m: Matlab compatible version.
* NEWS: Mention break in backward compatibility.
* scripts/deprecated/javafields.m: Modify call to strsplit().
* scripts/deprecated/javamethods.m: ditto
* scripts/general/fieldnames.m: ditto
* scripts/general/int2str.m: ditto
* scripts/general/methods.m: ditto
* scripts/general/num2str.m: ditto
* scripts/help/gen_doc_cache.m: ditto
* scripts/help/help.m: ditto
* scripts/help/lookfor.m: ditto
* scripts/io/strread.m: ditto
* scripts/java/javaclasspath.m: ditto
* scripts/miscellaneous/compare_versions.m: ditto
* scripts/miscellaneous/computer.m: ditto
* scripts/miscellaneous/fact.m: ditto
* scripts/miscellaneous/tar.m: ditto
* scripts/miscellaneous/unpack.m: ditto
* scripts/miscellaneous/what.m: ditto
* scripts/miscellaneous/zip.m: ditto
* scripts/pkg/private/configure_make.m: ditto
* scripts/pkg/private/fix_depends.m: ditto
* scripts/pkg/private/generate_lookfor_cache.m: ditto
* scripts/pkg/private/list_forge_packages.m: ditto
* scripts/pkg/private/unload_packages.m: ditto
* scripts/pkg/private/write_index.m: ditto
* scripts/plot/private/__file_filter__.m: ditto
* scripts/plot/private/__fltk_file_filter__.m: ditto
* scripts/plot/private/__go_draw_axes__.m: ditto
* scripts/plot/private/__next_line_style__.m: ditto
* scripts/strings/untabify.m: ditto
* scripts/testfun/rundemos.m: ditto
* scripts/testfun/runtests.m: ditto
author | Ben Abbott <bpabbott@mac.com> |
---|---|
date | Sun, 31 Mar 2013 19:19:04 -0400 |
parents | f59797321a1b |
children | 9336d0e1d1ec |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
12815
diff
changeset
|
1 ## Copyright (C) 2001-2012 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 ## | |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
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 | |
15899
f59797321a1b
perms.m: Match documentation variable names to function variable names.
Rik <rik@octave.org>
parents:
15564
diff
changeset
|
41 function A = perms (v) |
6391 | 42 if (nargin != 1) |
43 print_usage (); | |
44 endif | |
15899
f59797321a1b
perms.m: Match documentation variable names to function variable names.
Rik <rik@octave.org>
parents:
15564
diff
changeset
|
45 vidx = [1:length(v)]'; |
f59797321a1b
perms.m: Match documentation variable names to function variable names.
Rik <rik@octave.org>
parents:
15564
diff
changeset
|
46 n = length (vidx); |
9836 | 47 |
48 if (n == 0) | |
15564
ed6385e23420
perms.m: make it work for string arguments
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14872
diff
changeset
|
49 p = []; |
5827 | 50 else |
15899
f59797321a1b
perms.m: Match documentation variable names to function variable names.
Rik <rik@octave.org>
parents:
15564
diff
changeset
|
51 p = vidx(1); |
9836 | 52 for j = 2:n |
15564
ed6385e23420
perms.m: make it work for string arguments
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14872
diff
changeset
|
53 B = p; |
ed6385e23420
perms.m: make it work for string arguments
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14872
diff
changeset
|
54 p = zeros (prod (2:j), n); |
14872
c2dbdeaa25df
maint: use rows() and columns() to clarify m-files.
Rik <octave@nomad.inbox5.com>
parents:
14868
diff
changeset
|
55 k = rows (B); |
9836 | 56 idx = 1:k; |
57 for i = j:-1:1 | |
15564
ed6385e23420
perms.m: make it work for string arguments
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14872
diff
changeset
|
58 p(idx,1:i-1) = B(:,1:i-1); |
15899
f59797321a1b
perms.m: Match documentation variable names to function variable names.
Rik <rik@octave.org>
parents:
15564
diff
changeset
|
59 p(idx,i) = vidx(j); |
15564
ed6385e23420
perms.m: make it work for string arguments
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14872
diff
changeset
|
60 p(idx,i+1:j) = B(:,i:j-1); |
9836 | 61 idx += k; |
62 endfor | |
5827 | 63 endfor |
64 endif | |
15899
f59797321a1b
perms.m: Match documentation variable names to function variable names.
Rik <rik@octave.org>
parents:
15564
diff
changeset
|
65 A = v(p); |
5827 | 66 endfunction |
12815
918610ea2f34
codesprint: new tests for specfun directory
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
67 |
918610ea2f34
codesprint: new tests for specfun directory
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
68 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
69 %!assert (perms ([1,2,3]), [1,2,3;2,1,3;1,3,2;2,3,1;3,1,2;3,2,1]) |
15564
ed6385e23420
perms.m: make it work for string arguments
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14872
diff
changeset
|
70 %!assert (perms ("abc"), ["abc"; "bac"; "acb"; "bca"; "cab"; "cba"]) |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
71 %!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])) |
12815
918610ea2f34
codesprint: new tests for specfun directory
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
72 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
73 %!error perms () |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
74 %!error perms (1, 2) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
75 |