]> Creatis software - CreaPhase.git/blob - octave_packages/m/general/rot90.m
update packages
[CreaPhase.git] / octave_packages / m / general / rot90.m
1 ## Copyright (C) 1993-2012 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 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} {} rot90 (@var{A})
21 ## @deftypefnx {Function File} {} rot90 (@var{A}, @var{k})
22 ## Return a copy of @var{A} 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{k} 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 ## rot90 ([1, 2; 3, 4], 3)
44 ## rot90 ([1, 2; 3, 4], 7)
45 ## @end group
46 ## @end example
47 ##
48 ## Note that @code{rot90} only works with 2-D arrays.  To rotate N-D arrays
49 ## use @code{rotdim} instead.
50 ## @seealso{rotdim, flipud, fliplr, flipdim}
51 ## @end deftypefn
52
53 ## Author: jwe
54
55 function B = rot90 (A, k = 1)
56
57   if (nargin < 1 || nargin > 2)
58     print_usage ();
59   endif
60
61   if (ndims (A) > 2)
62     error ("rot90: A must be a 2-D array");
63   elseif (! (isscalar (k) && isreal (k) && k == fix (k)))
64     error ("rot90: K must be a single real integer");
65   endif
66
67   k = mod (k, 4);
68
69   if (k == 0)
70     B = A;
71   elseif (k == 1)
72     B = flipud (A.');
73   elseif (k == 2)
74     B = flipud (fliplr (A));
75   elseif (k == 3)
76     B = (flipud (A)).';
77   else
78     error ("rot90: internal error!");
79   endif
80
81 endfunction
82
83
84 %!test
85 %! x1 = [1, 2; 3, 4];
86 %! x2 = [2, 4; 1, 3];
87 %! x3 = [4, 3; 2, 1];
88 %! x4 = [3, 1; 4, 2];
89 %!
90 %! assert(rot90 (x1), x2);
91 %! assert(rot90 (x1, 2), x3);
92 %! assert(rot90 (x1, 3), x4);
93 %! assert(rot90 (x1, 4), x1);
94 %! assert(rot90 (x1, 5), x2);
95 %! assert(rot90 (x1, -1), x4);
96
97 %% Test input validation
98 %!error rot90 ();
99 %!error rot90 (1, 2, 3);
100 %!error rot90 (1, ones(2));
101 %!error rot90 (1, 1.5);
102 %!error rot90 (1, 1+i);