annotate scripts/general/shift.m @ 3784:097af73b5f4f

[project @ 2001-02-07 17:17:13 by jwe]
author jwe
date Wed, 07 Feb 2001 17:17:30 +0000
parents e031284eea27
children 38c61cbf086c
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
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3369
diff changeset
2 ##
2539
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.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3369
diff changeset
7 ##
2539
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
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3369
diff changeset
11 ## General Public License for more details.
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3369
diff changeset
12 ##
2539
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
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3263
diff changeset
17 ## -*- texinfo -*-
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3263
diff changeset
18 ## @deftypefn {Function File} {} shift (@var{x}, @var{b})
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3263
diff changeset
19 ## If @var{x} is a vector, perform a circular shift of length @var{b} of
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3263
diff changeset
20 ## the elements of @var{x}.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3369
diff changeset
21 ##
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3263
diff changeset
22 ## If @var{x} is a matrix, do the same for each column of @var{x}.
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3263
diff changeset
23 ## @end deftypefn
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
24
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
25 ## Author: AW <Andreas.Weingessel@ci.tuwien.ac.at>
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
26 ## Created: 14 September 1994
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
27 ## Adapted-By: jwe
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
28
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
29 function y = shift (x, b)
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3369
diff changeset
30
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
31 if (nargin != 2)
3457
e031284eea27 [project @ 2000-01-19 08:49:56 by jwe]
jwe
parents: 3426
diff changeset
32 usage ("shift (X, b)");
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
33 endif
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
34
3238
041ea33fbbf4 [project @ 1999-03-26 17:48:16 by jwe]
jwe
parents: 2539
diff changeset
35 [nr, nc] = size (x);
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3369
diff changeset
36
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
37 if (nr == 0 || nc == 0)
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
38 error ("shift: x must not be empty");
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
39 elseif (nr == 1)
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
40 x = x.';
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
41 nr = nc;
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
42 nc = 0;
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
43 endif
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
44
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
45 if (! (is_scalar (b) && b == round (b)))
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
46 error ("shift: b must be an integer");
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
47 endif
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
48
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
49 if (b >= 0)
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
50 b = rem (b, nr);
3238
041ea33fbbf4 [project @ 1999-03-26 17:48:16 by jwe]
jwe
parents: 2539
diff changeset
51 t1 = x (nr-b+1:nr, :);
041ea33fbbf4 [project @ 1999-03-26 17:48:16 by jwe]
jwe
parents: 2539
diff changeset
52 t2 = x (1:nr-b, :);
041ea33fbbf4 [project @ 1999-03-26 17:48:16 by jwe]
jwe
parents: 2539
diff changeset
53 y = [t1; t2];
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
54 elseif (b < 0)
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
55 b = rem (abs (b), nr);
3263
7d80b56e0dc8 [project @ 1999-08-20 19:10:09 by jwe]
jwe
parents: 3238
diff changeset
56 t1 = x (b+1:nr, :);
3238
041ea33fbbf4 [project @ 1999-03-26 17:48:16 by jwe]
jwe
parents: 2539
diff changeset
57 t2 = x (1:b, :);
041ea33fbbf4 [project @ 1999-03-26 17:48:16 by jwe]
jwe
parents: 2539
diff changeset
58 y = [t1; t2];
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
59 endif
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
60
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
61 if (nc == 0)
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
62 y = reshape (y, 1, nr);
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
63 endif
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
64
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
65 endfunction