2539
|
1 ## Copyright (C) 1995, 1996 Kurt Hornik |
3426
|
2 ## |
2539
|
3 ## This program is free software; you can redistribute it and/or modify |
|
4 ## it under the terms of the GNU General Public License as published by |
|
5 ## the Free Software Foundation; either version 2, or (at your option) |
|
6 ## any later version. |
3426
|
7 ## |
2539
|
8 ## This program is distributed in the hope that it will be useful, but |
|
9 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
11 ## General Public License for more details. |
|
12 ## |
2539
|
13 ## You should have received a copy of the GNU General Public License |
|
14 ## along with this file. If not, write to the Free Software Foundation, |
|
15 ## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
16 |
3369
|
17 ## -*- texinfo -*- |
|
18 ## @deftypefn {Function File} {} shift (@var{x}, @var{b}) |
|
19 ## If @var{x} is a vector, perform a circular shift of length @var{b} of |
|
20 ## the elements of @var{x}. |
3426
|
21 ## |
3369
|
22 ## If @var{x} is a matrix, do the same for each column of @var{x}. |
|
23 ## @end deftypefn |
2539
|
24 |
|
25 ## Author: AW <Andreas.Weingessel@ci.tuwien.ac.at> |
|
26 ## Created: 14 September 1994 |
|
27 ## Adapted-By: jwe |
|
28 |
|
29 function y = shift (x, b) |
3426
|
30 |
2539
|
31 if (nargin != 2) |
3457
|
32 usage ("shift (X, b)"); |
2539
|
33 endif |
|
34 |
3238
|
35 [nr, nc] = size (x); |
3426
|
36 |
2539
|
37 if (nr == 0 || nc == 0) |
|
38 error ("shift: x must not be empty"); |
|
39 elseif (nr == 1) |
|
40 x = x.'; |
|
41 nr = nc; |
|
42 nc = 0; |
|
43 endif |
|
44 |
|
45 if (! (is_scalar (b) && b == round (b))) |
|
46 error ("shift: b must be an integer"); |
|
47 endif |
|
48 |
|
49 if (b >= 0) |
|
50 b = rem (b, nr); |
3238
|
51 t1 = x (nr-b+1:nr, :); |
|
52 t2 = x (1:nr-b, :); |
|
53 y = [t1; t2]; |
2539
|
54 elseif (b < 0) |
|
55 b = rem (abs (b), nr); |
3263
|
56 t1 = x (b+1:nr, :); |
3238
|
57 t2 = x (1:b, :); |
|
58 y = [t1; t2]; |
2539
|
59 endif |
|
60 |
|
61 if (nc == 0) |
|
62 y = reshape (y, 1, nr); |
|
63 endif |
|
64 |
|
65 endfunction |