comparison scripts/general/shift.m @ 2539:1dca28c213f0

[project @ 1996-11-19 23:54:48 by jwe]
author jwe
date Tue, 19 Nov 1996 23:55:06 +0000
parents
children 041ea33fbbf4
comparison
equal deleted inserted replaced
2538:6f71af650490 2539:1dca28c213f0
1 ## Copyright (C) 1995, 1996 Kurt Hornik
2 ##
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.
7 ##
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
11 ## General Public License for more details.
12 ##
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
17 ## usage: y = shift (x, b)
18 ##
19 ## If x is a vector, perform a circular shift of length b of the
20 ## elements of x.
21 ##
22 ## If x is a matrix, do the same for each column of x.
23
24 ## Author: AW <Andreas.Weingessel@ci.tuwien.ac.at>
25 ## Created: 14 September 1994
26 ## Adapted-By: jwe
27
28 function y = shift (x, b)
29
30 if (nargin != 2)
31 error ("usage: shift (X, b)");
32 endif
33
34 [nr nc] = size (x);
35
36 if (nr == 0 || nc == 0)
37 error ("shift: x must not be empty");
38 elseif (nr == 1)
39 x = x.';
40 nr = nc;
41 nc = 0;
42 endif
43
44 if (! (is_scalar (b) && b == round (b)))
45 error ("shift: b must be an integer");
46 endif
47
48 if (b >= 0)
49 b = rem (b, nr);
50 y = [x (nr - b + 1 : nr, :); x (1 : nr - b, :)];
51 elseif (b < 0)
52 b = rem (abs (b), nr);
53 y = [x (b + 1 : nr, :); x (1 : b, :)];
54 endif
55
56 if (nc == 0)
57 y = reshape (y, 1, nr);
58 endif
59
60 endfunction