1 ## Copyright (C) 1995-2012 Kurt Hornik
3 ## This file is part of Octave.
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.
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.
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/>.
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}.
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
30 ## Author: AW <Andreas.Weingessel@ci.tuwien.ac.at>
31 ## Created: 14 September 1994
34 function y = shift (x, b, dim)
36 if (nargin != 2 && nargin != 3)
41 error ("shift: X must not be empty");
42 elseif (! (isscalar (b) && b == fix (b)))
43 error ("shift: B must be an integer");
50 if (!(isscalar (dim) && dim == fix (dim))
51 || !(1 <= dim && dim <= nd))
52 error ("shift: DIM must be an integer and a valid dimension");
55 ## Find the first non-singleton dimension.
56 (dim = find (sz > 1, 1)) || (dim = 1);
61 idx = repmat ({':'}, nd, 1);
64 idx{dim} = [d-b+1:d, 1:d-b];
67 idx{dim} = [b+1:d, 1:b];
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]);
90 %% Test input validation
92 %!error shift (1, 2, 3, 4)
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)