Mercurial > hg > octave-shane
changeset 15564:ed6385e23420
perms.m: make it work for string arguments
author | Jordi Gutiérrez Hermoso <jordigh@octave.org> |
---|---|
date | Fri, 26 Oct 2012 10:46:17 -0400 |
parents | 8ed107220a3e |
children | de751531e548 81ff500bfb4e |
files | scripts/specfun/perms.m |
diffstat | 1 files changed, 11 insertions(+), 10 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/specfun/perms.m +++ b/scripts/specfun/perms.m @@ -38,35 +38,36 @@ ## @end example ## @end deftypefn -function A = perms (v) +function A = perms (w) if (nargin != 1) print_usage (); endif - v = v(:); + v = [1:length(w)]'; n = length (v); if (n == 0) - A = []; + p = []; else - A = v(1); + p = v(1); for j = 2:n - B = A; - A = zeros (prod (2:j), n, class (v)); + B = p; + p = zeros (prod (2:j), n); k = rows (B); idx = 1:k; for i = j:-1:1 - A(idx,1:i-1) = B(:,1:i-1); - A(idx,i) = v(j); - A(idx,i+1:j) = B(:,i:j-1); + p(idx,1:i-1) = B(:,1:i-1); + p(idx,i) = v(j); + p(idx,i+1:j) = B(:,i:j-1); idx += k; endfor endfor endif + A = w(p); endfunction %!assert (perms ([1,2,3]), [1,2,3;2,1,3;1,3,2;2,3,1;3,1,2;3,2,1]) -%!assert (perms (1:3), perms ([1,2,3])) +%!assert (perms ("abc"), ["abc"; "bac"; "acb"; "bca"; "cab"; "cba"]) %!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])) %!error perms ()