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

  1. ## Copyright (C) 1996, 1997 John W. Eaton
  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 2, or (at your option)
  8. ## 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, write to the Free
  17. ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  18. ## 02111-1307, USA.
  19.  
  20. ## -*- texinfo -*-
  21. ## @deftypefn {Function File} {} rot90 (@var{x}, @var{n})
  22. ## Return a copy of @var{x} with the elements rotated counterclockwise in
  23. ## 90-degree increments.  The second argument is optional, and specifies
  24. ## how many 90-degree rotations are to be applied (the default value is 1).
  25. ## Negative values of @var{n} rotate the matrix in a clockwise direction.
  26. ## For example,
  27. ## 
  28. ## @example
  29. ## @group
  30. ## rot90 ([1, 2; 3, 4], -1)
  31. ##      @result{}  3  1
  32. ##          4  2
  33. ## @end group
  34. ## @end example
  35. ## 
  36. ## @noindent
  37. ## rotates the given matrix clockwise by 90 degrees.  The following are all
  38. ## equivalent statements:
  39. ## 
  40. ## @example
  41. ## @group
  42. ## rot90 ([1, 2; 3, 4], -1)
  43. ## @equiv{}
  44. ## rot90 ([1, 2; 3, 4], 3)
  45. ## @equiv{}
  46. ## rot90 ([1, 2; 3, 4], 7)
  47. ## @end group
  48. ## @end example
  49. ## @end deftypefn
  50.  
  51. ## See also: flipud, fliplr
  52.  
  53. ## Author: jwe
  54.  
  55. function y = rot90 (x, k)
  56.  
  57.   if (nargin < 2)
  58.     k = 1;
  59.   endif
  60.  
  61.   if (imag (k) != 0 || fix (k) != k)
  62.     error ("rot90: k must be an integer");
  63.   endif
  64.  
  65.   if (nargin == 1 || nargin == 2)
  66.     k = rem (k, 4);
  67.     if (k < 0)
  68.       k = k + 4;
  69.     endif
  70.     if (k == 0)
  71.       y = x;
  72.     elseif (k == 1)
  73.       y = flipud (x.');
  74.     elseif (k == 2)
  75.       y = flipud (fliplr (x));
  76.     elseif (k == 3)
  77.       y = (flipud (x)).';
  78.     else
  79.       error ("rot90: internal error!");
  80.     endif
  81.   else
  82.     usage ("rot90 (x [, k])");
  83.   endif
  84.  
  85. endfunction
  86.