annotate scripts/general/shift.m @ 3263:7d80b56e0dc8

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