Mercurial > hg > octave-nkf
annotate scripts/general/shift.m @ 11120:a44f979a35ce
style fixes for some .m files
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 20 Oct 2010 20:49:17 -0400 |
parents | 35adf2a71f3f |
children | fe3c3dfc07eb |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1995, 1996, 1999, 2000, 2002, 2003, 2004, 2005, 2006, |
8920 | 2 ## 2007, 2008 Kurt Hornik |
3426 | 3 ## |
3922 | 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. | |
3426 | 10 ## |
3922 | 11 ## Octave is distributed in the hope that it will be useful, but |
2539 | 12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
3426 | 14 ## General Public License for more details. |
15 ## | |
2539 | 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/>. | |
2539 | 19 |
3369 | 20 ## -*- texinfo -*- |
10690
35adf2a71f3f
Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
21 ## @deftypefn {Function File} {} shift (@var{x}, @var{b}) |
4862 | 22 ## @deftypefnx {Function File} {} shift (@var{x}, @var{b}, @var{dim}) |
3369 | 23 ## If @var{x} is a vector, perform a circular shift of length @var{b} of |
24 ## the elements of @var{x}. | |
3426 | 25 ## |
3369 | 26 ## If @var{x} is a matrix, do the same for each column of @var{x}. |
4862 | 27 ## If the optional @var{dim} argument is given, operate along this |
10690
35adf2a71f3f
Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
28 ## dimension. |
3369 | 29 ## @end deftypefn |
2539 | 30 |
31 ## Author: AW <Andreas.Weingessel@ci.tuwien.ac.at> | |
32 ## Created: 14 September 1994 | |
33 ## Adapted-By: jwe | |
34 | |
4862 | 35 function y = shift (x, b, dim) |
2539 | 36 |
4862 | 37 if (nargin != 2 && nargin != 3) |
6046 | 38 print_usage (); |
2539 | 39 endif |
40 | |
4030 | 41 if (! (isscalar (b) && b == round (b))) |
10690
35adf2a71f3f
Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
42 error ("shift: B must be an integer"); |
2539 | 43 endif |
44 | |
4862 | 45 nd = ndims (x); |
46 sz = size (x); | |
47 | |
48 if (nargin == 3) | |
10690
35adf2a71f3f
Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
49 if (!(isscalar (dim) && dim == round (dim)) || |
35adf2a71f3f
Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
50 !(1 <= dim && dim <= nd)) |
35adf2a71f3f
Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
51 error ("shift: DIM must be an integer and a valid dimension"); |
4862 | 52 endif |
53 else | |
10690
35adf2a71f3f
Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
54 ## Find the first non-singleton dimension. |
35adf2a71f3f
Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
55 dim = find (sz > 1, 1); |
35adf2a71f3f
Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
56 if (isempty (dim)) |
4862 | 57 dim = 1; |
58 endif | |
59 endif | |
60 | |
61 if (numel (x) < 1) | |
10690
35adf2a71f3f
Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
62 error ("shift: X must not be empty"); |
4862 | 63 endif |
64 | |
65 d = sz (dim); | |
66 | |
5568 | 67 idx = cell (); |
68 for i = 1:nd | |
7208 | 69 idx{i} = 1:sz(i); |
5568 | 70 endfor |
71 if (b >= 0) | |
72 b = rem (b, d); | |
7208 | 73 idx{dim} = [d-b+1:d, 1:d-b]; |
5568 | 74 elseif (b < 0) |
75 b = rem (abs (b), d); | |
7208 | 76 idx{dim} = [b+1:d, 1:b]; |
5568 | 77 endif |
7208 | 78 y = x(idx{:}); |
4369 | 79 |
7411 | 80 endfunction |
2539 | 81 |
7411 | 82 %!test |
83 %! a = [1, 2, 3]; | |
84 %! b = [4, 5, 6]; | |
85 %! c = [7, 8, 9]; | |
86 %! | |
87 %! r = [a, b, c]; | |
88 %! m = [a; b; c]; | |
89 %! | |
90 %! assert((shift (r, 3) == [c, a, b] | |
91 %! && shift (r, -6) == [c, a, b] | |
92 %! && shift (r, -3) == [b, c, a] | |
93 %! && shift (m, 1) == [c; a; b] | |
94 %! && shift (m, -2) == [c; a; b])); | |
95 | |
96 %!error shift (); | |
97 | |
98 %!error shift (1, 2, 3, 4); | |
99 |