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