]> Creatis software - CreaPhase.git/blob - octave_packages/m/general/shift.m
update packages
[CreaPhase.git] / octave_packages / m / general / shift.m
1 ## Copyright (C) 1995-2012 Kurt Hornik
2 ##
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
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn  {Function File} {} shift (@var{x}, @var{b})
21 ## @deftypefnx {Function File} {} shift (@var{x}, @var{b}, @var{dim})
22 ## If @var{x} is a vector, perform a circular shift of length @var{b} of
23 ## the elements of @var{x}.
24 ##
25 ## If @var{x} is a matrix, do the same for each column of @var{x}.
26 ## If the optional @var{dim} argument is given, operate along this
27 ## dimension.
28 ## @end deftypefn
29
30 ## Author: AW <Andreas.Weingessel@ci.tuwien.ac.at>
31 ## Created: 14 September 1994
32 ## Adapted-By: jwe
33
34 function y = shift (x, b, dim)
35
36   if (nargin != 2 && nargin != 3)
37     print_usage ();
38   endif
39
40   if (numel (x) < 1)
41     error ("shift: X must not be empty");
42   elseif (! (isscalar (b) && b == fix (b)))
43     error ("shift: B must be an integer");
44   endif
45
46   nd = ndims (x);
47   sz = size (x);
48
49   if (nargin == 3)
50     if (!(isscalar (dim) && dim == fix (dim))
51         || !(1 <= dim && dim <= nd))
52       error ("shift: DIM must be an integer and a valid dimension");
53     endif
54   else
55     ## Find the first non-singleton dimension.
56     (dim = find (sz > 1, 1)) || (dim = 1);
57   endif
58
59   d = sz(dim);
60
61   idx = repmat ({':'}, nd, 1);
62   if (b > 0)
63     b = rem (b, d);
64     idx{dim} = [d-b+1:d, 1:d-b];
65   elseif (b < 0)
66     b = rem (abs (b), d);
67     idx{dim} = [b+1:d, 1:b];
68   endif
69
70   y = x(idx{:});
71
72 endfunction
73
74
75 %!test
76 %! a = [1, 2, 3];
77 %! b = [4, 5, 6];
78 %! c = [7, 8, 9];
79 %!
80 %! r = [a, b, c];
81 %! m = [a; b; c];
82 %!
83 %! assert(shift (r, 0), r);
84 %! assert(shift (r, 3), [c, a, b]);
85 %! assert(shift (r, -6), [c, a, b]);
86 %! assert(shift (r, -3), [b, c, a]);
87 %! assert(shift (m, 1), [c; a; b]);
88 %! assert(shift (m, -2), [c; a; b]);
89
90 %% Test input validation
91 %!error shift ()
92 %!error shift (1, 2, 3, 4)
93 %!error shift ([], 1)
94 %!error shift (ones(2), ones(2))
95 %!error shift (ones(2), 1.5)
96 %!error shift (1, 1, 1.5)
97 %!error shift (1, 1, 0)
98 %!error shift (1, 1, 3)
99