2539
|
1 ## Copyright (C) 1995, 1996 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 -*- |
|
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}. |
4862
|
26 ## If the optional @var{dim} argument is given, operate along this |
|
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))) |
2539
|
41 error ("shift: b must be an integer"); |
|
42 endif |
|
43 |
4862
|
44 nd = ndims (x); |
|
45 sz = size (x); |
|
46 |
|
47 if (nargin == 3) |
|
48 if (! (isscalar (dim) && dim == round (dim)) && dim > 0 && |
|
49 dim < (nd + 1)) |
|
50 error ("shift: dim must be an integer and valid dimension"); |
|
51 endif |
|
52 else |
5568
|
53 ## Find the first non-singleton dimension |
4862
|
54 dim = 1; |
|
55 while (dim < nd + 1 && sz (dim) == 1) |
|
56 dim = dim + 1; |
|
57 endwhile |
|
58 if (dim > nd) |
|
59 dim = 1; |
|
60 endif |
|
61 endif |
|
62 |
|
63 if (numel (x) < 1) |
|
64 error ("shift: x must not be empty"); |
|
65 endif |
|
66 |
|
67 d = sz (dim); |
|
68 |
5568
|
69 idx = cell (); |
|
70 for i = 1:nd |
|
71 idx {i} = 1:sz(i); |
|
72 endfor |
|
73 if (b >= 0) |
|
74 b = rem (b, d); |
|
75 idx {dim} = [d-b+1:d, 1:d-b]; |
|
76 elseif (b < 0) |
|
77 b = rem (abs (b), d); |
|
78 idx {dim} = [b+1:d, 1:b]; |
|
79 endif |
|
80 y = x (idx {:}); |
4369
|
81 |
2539
|
82 |
|
83 endfunction |