Mercurial > hg > octave-nkf
annotate scripts/general/shift.m @ 11587:c792872f8942
all script files: untabify and strip trailing whitespace
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 20 Jan 2011 17:35:29 -0500 |
parents | fd0a3ac60b0e |
children | 9493880928c8 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 1995-2011 Kurt Hornik |
3426 | 2 ## |
3922 | 3 ## This file is part of Octave. |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
3426 | 9 ## |
3922 | 10 ## Octave is distributed in the hope that it will be useful, but |
2539 | 11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
3426 | 13 ## General Public License for more details. |
14 ## | |
2539 | 15 ## You should have received a copy of the GNU General Public License |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
2539 | 18 |
3369 | 19 ## -*- texinfo -*- |
10690
35adf2a71f3f
Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
20 ## @deftypefn {Function File} {} shift (@var{x}, @var{b}) |
4862 | 21 ## @deftypefnx {Function File} {} shift (@var{x}, @var{b}, @var{dim}) |
3369 | 22 ## If @var{x} is a vector, perform a circular shift of length @var{b} of |
23 ## the elements of @var{x}. | |
3426 | 24 ## |
3369 | 25 ## If @var{x} is a matrix, do the same for each column of @var{x}. |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
26 ## 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
|
27 ## dimension. |
3369 | 28 ## @end deftypefn |
2539 | 29 |
30 ## Author: AW <Andreas.Weingessel@ci.tuwien.ac.at> | |
31 ## Created: 14 September 1994 | |
32 ## Adapted-By: jwe | |
33 | |
4862 | 34 function y = shift (x, b, dim) |
2539 | 35 |
4862 | 36 if (nargin != 2 && nargin != 3) |
6046 | 37 print_usage (); |
2539 | 38 endif |
39 | |
4030 | 40 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
|
41 error ("shift: B must be an integer"); |
2539 | 42 endif |
43 | |
4862 | 44 nd = ndims (x); |
45 sz = size (x); | |
46 | |
47 if (nargin == 3) | |
11149
fe3c3dfc07eb
style fix: break lines before && and ||, not after
John W. Eaton <jwe@octave.org>
parents:
10690
diff
changeset
|
48 if (!(isscalar (dim) && dim == round (dim)) |
fe3c3dfc07eb
style fix: break lines before && and ||, not after
John W. Eaton <jwe@octave.org>
parents:
10690
diff
changeset
|
49 || !(1 <= dim && dim <= nd)) |
10690
35adf2a71f3f
Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
50 error ("shift: DIM must be an integer and a valid dimension"); |
4862 | 51 endif |
52 else | |
10690
35adf2a71f3f
Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
53 ## 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
|
54 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
|
55 if (isempty (dim)) |
4862 | 56 dim = 1; |
57 endif | |
58 endif | |
59 | |
60 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
|
61 error ("shift: X must not be empty"); |
4862 | 62 endif |
63 | |
64 d = sz (dim); | |
65 | |
5568 | 66 idx = cell (); |
67 for i = 1:nd | |
7208 | 68 idx{i} = 1:sz(i); |
5568 | 69 endfor |
70 if (b >= 0) | |
71 b = rem (b, d); | |
7208 | 72 idx{dim} = [d-b+1:d, 1:d-b]; |
5568 | 73 elseif (b < 0) |
74 b = rem (abs (b), d); | |
7208 | 75 idx{dim} = [b+1:d, 1:b]; |
5568 | 76 endif |
7208 | 77 y = x(idx{:}); |
4369 | 78 |
7411 | 79 endfunction |
2539 | 80 |
7411 | 81 %!test |
82 %! a = [1, 2, 3]; | |
83 %! b = [4, 5, 6]; | |
84 %! c = [7, 8, 9]; | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
85 %! |
7411 | 86 %! r = [a, b, c]; |
87 %! m = [a; b; c]; | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
88 %! |
7411 | 89 %! assert((shift (r, 3) == [c, a, b] |
90 %! && shift (r, -6) == [c, a, b] | |
91 %! && shift (r, -3) == [b, c, a] | |
92 %! && shift (m, 1) == [c; a; b] | |
93 %! && shift (m, -2) == [c; a; b])); | |
94 | |
95 %!error shift (); | |
96 | |
97 %!error shift (1, 2, 3, 4); | |
98 |