annotate scripts/general/circshift.m @ 10549:95c3e38098bf

Untabify .m scripts
author Rik <code@nomad.inbox5.com>
date Fri, 23 Apr 2010 11:28:50 -0700
parents 16f53d29049f
children 693e22af08ae
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9245
16f53d29049f update copyright notices
John W. Eaton <jwe@octave.org>
parents: 9051
diff changeset
1 ## Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 David Bateman
4894
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
2 ##
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
3 ## This file is part of Octave.
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
4 ##
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6671
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6671
diff changeset
8 ## your option) any later version.
4894
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
9 ##
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
13 ## General Public License for more details.
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
14 ##
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6671
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6671
diff changeset
17 ## <http://www.gnu.org/licenses/>.
4894
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
18
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
19 ## -*- texinfo -*-
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6046
diff changeset
20 ## @deftypefn {Function File} {@var{y} =} circshift (@var{x}, @var{n})
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 9041
diff changeset
21 ## Circularly shifts the values of the array @var{x}. @var{n} must be
4894
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
22 ## a vector of integers no longer than the number of dimensions in
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 9041
diff changeset
23 ## @var{x}. The values of @var{n} can be either positive or negative,
4894
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
24 ## which determines the direction in which the values or @var{x} are
9041
853f96e8008f Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
25 ## shifted. If an element of @var{n} is zero, then the corresponding
853f96e8008f Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
26 ## dimension of @var{x} will not be shifted. For example
4894
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
27 ##
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
28 ## @example
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
29 ## @group
6671
9042b00fc45e [project @ 2007-05-30 06:33:29 by jwe]
jwe
parents: 6547
diff changeset
30 ## x = [1, 2, 3; 4, 5, 6; 7, 8, 9];
4894
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
31 ## circshift (x, 1)
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
32 ## @result{} 7, 8, 9
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
33 ## 1, 2, 3
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
34 ## 4, 5, 6
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
35 ## circshift (x, -2)
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
36 ## @result{} 7, 8, 9
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
37 ## 1, 2, 3
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
38 ## 4, 5, 6
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
39 ## circshift (x, [0,1])
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
40 ## @result{} 3, 1, 2
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
41 ## 6, 4, 5
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
42 ## 9, 7, 8
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
43 ## @end group
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
44 ## @end example
5642
2618a0750ae6 [project @ 2006-03-06 21:26:48 by jwe]
jwe
parents: 5307
diff changeset
45 ## @seealso {permute, ipermute, shiftdim}
4894
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
46 ## @end deftypefn
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
47
4895
9df0048ae0a1 [project @ 2004-06-03 19:34:07 by jwe]
jwe
parents: 4894
diff changeset
48 function y = circshift (x, n)
4894
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
49
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
50 if (nargin == 2)
7614
52f90c7adac6 Avoid infinite loop in circshift for infinite matrices
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
51 if (isempty (x))
52f90c7adac6 Avoid infinite loop in circshift for infinite matrices
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
52 y = x;
52f90c7adac6 Avoid infinite loop in circshift for infinite matrices
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
53 else
52f90c7adac6 Avoid infinite loop in circshift for infinite matrices
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
54 nd = ndims (x);
52f90c7adac6 Avoid infinite loop in circshift for infinite matrices
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
55 sz = size (x);
4894
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
56
7614
52f90c7adac6 Avoid infinite loop in circshift for infinite matrices
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
57 if (! isvector (n) && length (n) > nd)
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9245
diff changeset
58 error ("circshift: n must be a vector, no longer than the number of dimension in x");
7614
52f90c7adac6 Avoid infinite loop in circshift for infinite matrices
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
59 endif
4894
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
60
7614
52f90c7adac6 Avoid infinite loop in circshift for infinite matrices
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
61 if (any (n != floor (n)))
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9245
diff changeset
62 error ("circshift: all values of n must be integers");
7614
52f90c7adac6 Avoid infinite loop in circshift for infinite matrices
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
63 endif
4894
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
64
7614
52f90c7adac6 Avoid infinite loop in circshift for infinite matrices
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
65 idx = cell ();
52f90c7adac6 Avoid infinite loop in circshift for infinite matrices
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
66 for i = 1:length (n);
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9245
diff changeset
67 nn = n(i);
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9245
diff changeset
68 if (nn < 0)
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9245
diff changeset
69 while (sz(i) <= -nn)
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9245
diff changeset
70 nn = nn + sz(i);
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9245
diff changeset
71 endwhile
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9245
diff changeset
72 idx{i} = [(1-nn):sz(i), 1:-nn];
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9245
diff changeset
73 else
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9245
diff changeset
74 while (sz(i) <= nn)
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9245
diff changeset
75 nn = nn - sz(i);
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9245
diff changeset
76 endwhile
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9245
diff changeset
77 idx{i} = [(sz(i)-nn+1):sz(i), 1:(sz(i)-nn)];
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9245
diff changeset
78 endif
7614
52f90c7adac6 Avoid infinite loop in circshift for infinite matrices
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
79 endfor
52f90c7adac6 Avoid infinite loop in circshift for infinite matrices
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
80 for i = (length(n) + 1) : nd
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9245
diff changeset
81 idx{i} = 1:sz(i);
7614
52f90c7adac6 Avoid infinite loop in circshift for infinite matrices
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
82 endfor
52f90c7adac6 Avoid infinite loop in circshift for infinite matrices
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
83 y = x(idx{:});
52f90c7adac6 Avoid infinite loop in circshift for infinite matrices
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
84 endif
4894
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
85 else
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5642
diff changeset
86 print_usage ();
4894
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
87 endif
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
88 endfunction
7614
52f90c7adac6 Avoid infinite loop in circshift for infinite matrices
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
89
52f90c7adac6 Avoid infinite loop in circshift for infinite matrices
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
90 %!shared x
52f90c7adac6 Avoid infinite loop in circshift for infinite matrices
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
91 %! x = [1, 2, 3; 4, 5, 6; 7, 8, 9];
52f90c7adac6 Avoid infinite loop in circshift for infinite matrices
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
92
52f90c7adac6 Avoid infinite loop in circshift for infinite matrices
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
93 %!assert (circshift (x, 1), [7, 8, 9; 1, 2, 3; 4, 5, 6])
52f90c7adac6 Avoid infinite loop in circshift for infinite matrices
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
94 %!assert (circshift (x, -2), [7, 8, 9; 1, 2, 3; 4, 5, 6])
52f90c7adac6 Avoid infinite loop in circshift for infinite matrices
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
95 %!assert (circshift (x, [0, 1]), [3, 1, 2; 6, 4, 5; 9, 7, 8]);
52f90c7adac6 Avoid infinite loop in circshift for infinite matrices
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
96 %!assert (circshift ([],1), [])