comparison scripts/specfun/perms.m @ 15899:f59797321a1b

perms.m: Match documentation variable names to function variable names. * perms.m: Match documentation variable names to function variable names.
author Rik <rik@octave.org>
date Fri, 04 Jan 2013 14:14:41 -0800
parents ed6385e23420
children 9336d0e1d1ec
comparison
equal deleted inserted replaced
15898:a7d89366d7ed 15899:f59797321a1b
36 ## 3 2 1 36 ## 3 2 1
37 ## @end group 37 ## @end group
38 ## @end example 38 ## @end example
39 ## @end deftypefn 39 ## @end deftypefn
40 40
41 function A = perms (w) 41 function A = perms (v)
42 if (nargin != 1) 42 if (nargin != 1)
43 print_usage (); 43 print_usage ();
44 endif 44 endif
45 v = [1:length(w)]'; 45 vidx = [1:length(v)]';
46 n = length (v); 46 n = length (vidx);
47 47
48 if (n == 0) 48 if (n == 0)
49 p = []; 49 p = [];
50 else 50 else
51 p = v(1); 51 p = vidx(1);
52 for j = 2:n 52 for j = 2:n
53 B = p; 53 B = p;
54 p = zeros (prod (2:j), n); 54 p = zeros (prod (2:j), n);
55 k = rows (B); 55 k = rows (B);
56 idx = 1:k; 56 idx = 1:k;
57 for i = j:-1:1 57 for i = j:-1:1
58 p(idx,1:i-1) = B(:,1:i-1); 58 p(idx,1:i-1) = B(:,1:i-1);
59 p(idx,i) = v(j); 59 p(idx,i) = vidx(j);
60 p(idx,i+1:j) = B(:,i:j-1); 60 p(idx,i+1:j) = B(:,i:j-1);
61 idx += k; 61 idx += k;
62 endfor 62 endfor
63 endfor 63 endfor
64 endif 64 endif
65 A = w(p); 65 A = v(p);
66 endfunction 66 endfunction
67 67
68 68
69 %!assert (perms ([1,2,3]), [1,2,3;2,1,3;1,3,2;2,3,1;3,1,2;3,2,1]) 69 %!assert (perms ([1,2,3]), [1,2,3;2,1,3;1,3,2;2,3,1;3,1,2;3,2,1])
70 %!assert (perms ("abc"), ["abc"; "bac"; "acb"; "bca"; "cab"; "cba"]) 70 %!assert (perms ("abc"), ["abc"; "bac"; "acb"; "bca"; "cab"; "cba"])