7017
|
1 ## Copyright (C) 1995, 1996, 1999, 2000, 2002, 2003, 2004, 2005, 2006, |
|
2 ## 2007 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 -*- |
|
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 |
|
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))) |
2539
|
42 error ("shift: b must be an integer"); |
|
43 endif |
|
44 |
4862
|
45 nd = ndims (x); |
|
46 sz = size (x); |
|
47 |
|
48 if (nargin == 3) |
|
49 if (! (isscalar (dim) && dim == round (dim)) && dim > 0 && |
|
50 dim < (nd + 1)) |
|
51 error ("shift: dim must be an integer and valid dimension"); |
|
52 endif |
|
53 else |
5568
|
54 ## Find the first non-singleton dimension |
4862
|
55 dim = 1; |
|
56 while (dim < nd + 1 && sz (dim) == 1) |
|
57 dim = dim + 1; |
|
58 endwhile |
|
59 if (dim > nd) |
|
60 dim = 1; |
|
61 endif |
|
62 endif |
|
63 |
|
64 if (numel (x) < 1) |
|
65 error ("shift: x must not be empty"); |
|
66 endif |
|
67 |
|
68 d = sz (dim); |
|
69 |
5568
|
70 idx = cell (); |
|
71 for i = 1:nd |
7208
|
72 idx{i} = 1:sz(i); |
5568
|
73 endfor |
|
74 if (b >= 0) |
|
75 b = rem (b, d); |
7208
|
76 idx{dim} = [d-b+1:d, 1:d-b]; |
5568
|
77 elseif (b < 0) |
|
78 b = rem (abs (b), d); |
7208
|
79 idx{dim} = [b+1:d, 1:b]; |
5568
|
80 endif |
7208
|
81 y = x(idx{:}); |
4369
|
82 |
7411
|
83 endfunction |
2539
|
84 |
7411
|
85 %!test |
|
86 %! a = [1, 2, 3]; |
|
87 %! b = [4, 5, 6]; |
|
88 %! c = [7, 8, 9]; |
|
89 %! |
|
90 %! r = [a, b, c]; |
|
91 %! m = [a; b; c]; |
|
92 %! |
|
93 %! assert((shift (r, 3) == [c, a, b] |
|
94 %! && shift (r, -6) == [c, a, b] |
|
95 %! && shift (r, -3) == [b, c, a] |
|
96 %! && shift (m, 1) == [c; a; b] |
|
97 %! && shift (m, -2) == [c; a; b])); |
|
98 |
|
99 %!error shift (); |
|
100 |
|
101 %!error shift (1, 2, 3, 4); |
|
102 |