7017
|
1 ## Copyright (C) 1993, 1994, 1995, 1996, 1997, 1999, 2000, 2004, 2005, |
8920
|
2 ## 2006, 2007, 2008, 2009 John W. Eaton |
2313
|
3 ## |
|
4 ## This file is part of Octave. |
|
5 ## |
|
6 ## Octave is free software; you can redistribute it and/or modify it |
|
7 ## under the terms of the GNU General Public License as published by |
7016
|
8 ## the Free Software Foundation; either version 3 of the License, or (at |
|
9 ## your option) any later version. |
2313
|
10 ## |
|
11 ## Octave is distributed in the hope that it will be useful, but |
|
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 ## General Public License for more details. |
|
15 ## |
|
16 ## You should have received a copy of the GNU General Public License |
7016
|
17 ## along with Octave; see the file COPYING. If not, see |
|
18 ## <http://www.gnu.org/licenses/>. |
245
|
19 |
3369
|
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, |
3426
|
27 ## |
3369
|
28 ## @example |
|
29 ## @group |
|
30 ## rot90 ([1, 2; 3, 4], -1) |
|
31 ## @result{} 3 1 |
|
32 ## 4 2 |
|
33 ## @end group |
|
34 ## @end example |
3426
|
35 ## |
3369
|
36 ## @noindent |
|
37 ## rotates the given matrix clockwise by 90 degrees. The following are all |
|
38 ## equivalent statements: |
3426
|
39 ## |
3369
|
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 |
4869
|
47 ## |
|
48 ## Due to the difficulty of defining an axis about which to rotate the |
|
49 ## matrix @code{rot90} only work with 2-D arrays. To rotate N-d arrays |
|
50 ## use @code{rotdim} instead. |
5642
|
51 ## @seealso{rotdim, flipud, fliplr, flipdim} |
3369
|
52 ## @end deftypefn |
4
|
53 |
2314
|
54 ## Author: jwe |
|
55 |
2311
|
56 function y = rot90 (x, k) |
4
|
57 |
4869
|
58 if (nargin == 1 || nargin == 2) |
|
59 if (nargin < 2) |
|
60 k = 1; |
|
61 endif |
4
|
62 |
4869
|
63 if (ndims (x) > 2) |
8664
|
64 error ("rot90: Only works with 2-D arrays"); |
4869
|
65 endif |
4
|
66 |
4869
|
67 if (imag (k) != 0 || fix (k) != k) |
|
68 error ("rot90: k must be an integer"); |
|
69 endif |
|
70 |
4
|
71 k = rem (k, 4); |
4869
|
72 |
4
|
73 if (k < 0) |
|
74 k = k + 4; |
|
75 endif |
4869
|
76 |
4
|
77 if (k == 0) |
|
78 y = x; |
|
79 elseif (k == 1) |
3238
|
80 y = flipud (x.'); |
4
|
81 elseif (k == 2) |
|
82 y = flipud (fliplr (x)); |
|
83 elseif (k == 3) |
3238
|
84 y = (flipud (x)).'; |
4
|
85 else |
|
86 error ("rot90: internal error!"); |
|
87 endif |
|
88 else |
6046
|
89 print_usage (); |
4
|
90 endif |
|
91 |
|
92 endfunction |
7411
|
93 |
|
94 %!test |
|
95 %! x1 = [1, 2; |
|
96 %! 3, 4]; |
|
97 %! x2 = [2, 4; |
|
98 %! 1, 3]; |
|
99 %! x3 = [4, 3; |
|
100 %! 2, 1]; |
|
101 %! x4 = [3, 1; |
|
102 %! 4, 2]; |
|
103 %! |
|
104 %! assert((rot90 (x1)== x2 && rot90 (x1, 2) == x3 && rot90 (x1, 3) == x4 |
|
105 %! && rot90 (x1, 4) == x1 && rot90 (x1, 5) == x2 && rot90 (x1, -1) == x4)); |
|
106 |
|
107 %!error rot90 (); |
|
108 |
|
109 %!error rot90 (1, 2, 3); |
|
110 |