home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fb.zip / octave / SCRIPTS.ZIP / scripts / general / shift.m < prev    next >
Encoding:
Text File  |  1999-11-21  |  1.7 KB  |  66 lines

  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. ## -*- texinfo -*-
  18. ## @deftypefn {Function File} {} shift (@var{x}, @var{b})
  19. ## If @var{x} is a vector, perform a circular shift of length @var{b} of
  20. ## the elements of @var{x}.
  21. ## 
  22. ## If @var{x} is a matrix, do the same for each column of @var{x}.
  23. ## @end deftypefn
  24.  
  25. ## Author: AW <Andreas.Weingessel@ci.tuwien.ac.at>
  26. ## Created: 14 September 1994
  27. ## Adapted-By: jwe
  28.  
  29. function y = shift (x, b)
  30.   
  31.   if (nargin != 2)
  32.     error ("usage: shift (X, b)");
  33.   endif
  34.  
  35.   [nr, nc] = size (x);
  36.   
  37.   if (nr == 0 || nc == 0)
  38.     error ("shift: x must not be empty");
  39.   elseif (nr == 1)
  40.     x = x.';
  41.     nr = nc;
  42.     nc = 0;
  43.   endif
  44.  
  45.   if (! (is_scalar (b) && b == round (b)))
  46.     error ("shift: b must be an integer");
  47.   endif
  48.  
  49.   if (b >= 0)
  50.     b = rem (b, nr);
  51.     t1 = x (nr-b+1:nr, :);
  52.     t2 = x (1:nr-b, :);
  53.     y = [t1; t2];
  54.   elseif (b < 0)
  55.     b = rem (abs (b), nr);
  56.     t1 = x (b+1:nr, :);
  57.     t2 = x (1:b, :);
  58.     y = [t1; t2];
  59.   endif
  60.  
  61.   if (nc == 0)
  62.     y = reshape (y, 1, nr);
  63.   endif
  64.  
  65. endfunction
  66.