2847
|
1 ## Copyright (C) 1996, 1997 John W. Eaton |
2313
|
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 |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
2313
|
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 |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
245
|
18 |
3369
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {} rot90 (@var{x}, @var{n}) |
|
21 ## Return a copy of @var{x} with the elements rotated counterclockwise in |
|
22 ## 90-degree increments. The second argument is optional, and specifies |
|
23 ## how many 90-degree rotations are to be applied (the default value is 1). |
|
24 ## Negative values of @var{n} rotate the matrix in a clockwise direction. |
|
25 ## For example, |
3426
|
26 ## |
3369
|
27 ## @example |
|
28 ## @group |
|
29 ## rot90 ([1, 2; 3, 4], -1) |
|
30 ## @result{} 3 1 |
|
31 ## 4 2 |
|
32 ## @end group |
|
33 ## @end example |
3426
|
34 ## |
3369
|
35 ## @noindent |
|
36 ## rotates the given matrix clockwise by 90 degrees. The following are all |
|
37 ## equivalent statements: |
3426
|
38 ## |
3369
|
39 ## @example |
|
40 ## @group |
|
41 ## rot90 ([1, 2; 3, 4], -1) |
|
42 ## rot90 ([1, 2; 3, 4], 3) |
|
43 ## rot90 ([1, 2; 3, 4], 7) |
|
44 ## @end group |
|
45 ## @end example |
4869
|
46 ## |
|
47 ## Due to the difficulty of defining an axis about which to rotate the |
|
48 ## matrix @code{rot90} only work with 2-D arrays. To rotate N-d arrays |
|
49 ## use @code{rotdim} instead. |
5642
|
50 ## @seealso{rotdim, flipud, fliplr, flipdim} |
3369
|
51 ## @end deftypefn |
4
|
52 |
2314
|
53 ## Author: jwe |
|
54 |
2311
|
55 function y = rot90 (x, k) |
4
|
56 |
4869
|
57 if (nargin == 1 || nargin == 2) |
|
58 if (nargin < 2) |
|
59 k = 1; |
|
60 endif |
4
|
61 |
4869
|
62 if (ndims (x) > 2) |
|
63 error ("rot90: Only works with 2-D arrays") |
|
64 endif |
4
|
65 |
4869
|
66 if (imag (k) != 0 || fix (k) != k) |
|
67 error ("rot90: k must be an integer"); |
|
68 endif |
|
69 |
4
|
70 k = rem (k, 4); |
4869
|
71 |
4
|
72 if (k < 0) |
|
73 k = k + 4; |
|
74 endif |
4869
|
75 |
4
|
76 if (k == 0) |
|
77 y = x; |
|
78 elseif (k == 1) |
3238
|
79 y = flipud (x.'); |
4
|
80 elseif (k == 2) |
|
81 y = flipud (fliplr (x)); |
|
82 elseif (k == 3) |
3238
|
83 y = (flipud (x)).'; |
4
|
84 else |
|
85 error ("rot90: internal error!"); |
|
86 endif |
|
87 else |
6046
|
88 print_usage (); |
4
|
89 endif |
|
90 |
|
91 endfunction |