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 |
2539
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## 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 |
3922
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
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 |
|
72 idx {i} = 1:sz(i); |
|
73 endfor |
|
74 if (b >= 0) |
|
75 b = rem (b, d); |
|
76 idx {dim} = [d-b+1:d, 1:d-b]; |
|
77 elseif (b < 0) |
|
78 b = rem (abs (b), d); |
|
79 idx {dim} = [b+1:d, 1:b]; |
|
80 endif |
|
81 y = x (idx {:}); |
4369
|
82 |
2539
|
83 |
|
84 endfunction |