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 |
|
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 ## 02111-1307, USA. |
2539
|
19 |
3369
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} shift (@var{x}, @var{b}) |
|
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}. |
|
26 ## @end deftypefn |
2539
|
27 |
|
28 ## Author: AW <Andreas.Weingessel@ci.tuwien.ac.at> |
|
29 ## Created: 14 September 1994 |
|
30 ## Adapted-By: jwe |
|
31 |
|
32 function y = shift (x, b) |
3426
|
33 |
2539
|
34 if (nargin != 2) |
3457
|
35 usage ("shift (X, b)"); |
2539
|
36 endif |
|
37 |
3238
|
38 [nr, nc] = size (x); |
3426
|
39 |
2539
|
40 if (nr == 0 || nc == 0) |
|
41 error ("shift: x must not be empty"); |
|
42 elseif (nr == 1) |
|
43 x = x.'; |
|
44 nr = nc; |
|
45 nc = 0; |
|
46 endif |
|
47 |
4030
|
48 if (! (isscalar (b) && b == round (b))) |
2539
|
49 error ("shift: b must be an integer"); |
|
50 endif |
|
51 |
|
52 if (b >= 0) |
|
53 b = rem (b, nr); |
3238
|
54 t1 = x (nr-b+1:nr, :); |
|
55 t2 = x (1:nr-b, :); |
|
56 y = [t1; t2]; |
2539
|
57 elseif (b < 0) |
|
58 b = rem (abs (b), nr); |
3263
|
59 t1 = x (b+1:nr, :); |
3238
|
60 t2 = x (1:b, :); |
|
61 y = [t1; t2]; |
2539
|
62 endif |
|
63 |
|
64 if (nc == 0) |
|
65 y = reshape (y, 1, nr); |
|
66 endif |
|
67 |
|
68 endfunction |