7017
|
1 ## Copyright (C) 2004, 2005, 2006, 2007 David Bateman |
5012
|
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. |
5012
|
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/>. |
5012
|
18 |
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {} rotdim (@var{x}, @var{n}, @var{plane}) |
|
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 ## The third argument is also optional and defines the plane of the |
|
25 ## rotation. As such @var{plane} is a two element vector containing two |
|
26 ## different valid dimensions of the matrix. If @var{plane} is not given |
|
27 ## Then the first two non-singleton dimensions are used. |
|
28 ## |
|
29 ## Negative values of @var{n} rotate the matrix in a clockwise direction. |
|
30 ## For example, |
|
31 ## |
|
32 ## @example |
|
33 ## @group |
|
34 ## rotdim ([1, 2; 3, 4], -1, [1, 2]) |
|
35 ## @result{} 3 1 |
|
36 ## 4 2 |
|
37 ## @end group |
|
38 ## @end example |
|
39 ## |
|
40 ## @noindent |
|
41 ## rotates the given matrix clockwise by 90 degrees. The following are all |
|
42 ## equivalent statements: |
|
43 ## |
|
44 ## @example |
|
45 ## @group |
5487
|
46 ## rotdim ([1, 2; 3, 4], -1, [1, 2]) |
|
47 ## rotdim ([1, 2; 3, 4], 3, [1, 2]) |
|
48 ## rotdim ([1, 2; 3, 4], 7, [1, 2]) |
5012
|
49 ## @end group |
|
50 ## @end example |
5642
|
51 ## @seealso{rot90, flipud, fliplr, flipdim} |
5012
|
52 ## @end deftypefn |
|
53 |
|
54 function y = rotdim (x, k, plane) |
|
55 |
|
56 if (nargin < 1 || nargin > 3) |
6046
|
57 print_usage (); |
5012
|
58 endif |
|
59 |
5018
|
60 if (nargin > 1 && ! isempty(k)) |
5012
|
61 if (imag (k) != 0 || fix (k) != k) |
|
62 error ("rotdim: k must be an integer"); |
|
63 endif |
|
64 else |
|
65 k = 1; |
|
66 endif |
|
67 |
|
68 nd = ndims (x); |
|
69 sz = size (x); |
|
70 if (nargin < 3) |
|
71 ## Find the first two non-singleton dimension. |
|
72 plane = []; |
|
73 dim = 0; |
|
74 while (dim < nd) |
|
75 dim = dim + 1; |
|
76 if (sz (dim) != 1) |
|
77 plane = [plane, dim]; |
|
78 if (length (plane) == 2) |
|
79 break; |
|
80 endif |
|
81 endif |
|
82 endwhile |
|
83 if (length (plane) < 1) |
|
84 plane = [1, 2]; |
|
85 elseif (length (plane) < 2) |
|
86 plane = [1, plane]; |
|
87 endif |
|
88 else |
|
89 if (! (isvector (plane) && length (plane) == 2 |
|
90 && all (plane == round (plane)) && all (plane > 0) |
|
91 && all (plane < (nd + 1)) && plane(1) != plane(2))) |
|
92 error ("rotdim: plane must be a 2 element integer vector defining a valid plane"); |
|
93 endif |
|
94 endif |
|
95 |
|
96 k = rem (k, 4); |
|
97 if (k < 0) |
|
98 k = k + 4; |
|
99 endif |
|
100 if (k == 0) |
|
101 y = x; |
|
102 elseif (k == 2) |
5018
|
103 y = flipdim (flipdim (x, plane(1)), plane(2)); |
5012
|
104 elseif (k == 1 || k == 3) |
|
105 perm = 1:nd; |
|
106 perm(plane(1)) = plane(2); |
|
107 perm(plane(2)) = plane(1); |
|
108 y = permute (x, perm); |
|
109 if (k == 1) |
|
110 y = flipdim (y, min (plane)); |
|
111 else |
|
112 y = flipdim (y, max (plane)); |
|
113 endif |
|
114 else |
|
115 error ("rotdim: internal error!"); |
|
116 endif |
|
117 |
|
118 endfunction |