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 |
5487
|
47 ## rotdim ([1, 2; 3, 4], -1, [1, 2]) |
|
48 ## rotdim ([1, 2; 3, 4], 3, [1, 2]) |
|
49 ## rotdim ([1, 2; 3, 4], 7, [1, 2]) |
5012
|
50 ## @end group |
|
51 ## @end example |
5642
|
52 ## @seealso{rot90, flipud, fliplr, flipdim} |
5012
|
53 ## @end deftypefn |
|
54 |
|
55 function y = rotdim (x, k, plane) |
|
56 |
|
57 if (nargin < 1 || nargin > 3) |
6046
|
58 print_usage (); |
5012
|
59 endif |
|
60 |
5018
|
61 if (nargin > 1 && ! isempty(k)) |
5012
|
62 if (imag (k) != 0 || fix (k) != k) |
|
63 error ("rotdim: k must be an integer"); |
|
64 endif |
|
65 else |
|
66 k = 1; |
|
67 endif |
|
68 |
|
69 nd = ndims (x); |
|
70 sz = size (x); |
|
71 if (nargin < 3) |
|
72 ## Find the first two non-singleton dimension. |
|
73 plane = []; |
|
74 dim = 0; |
|
75 while (dim < nd) |
|
76 dim = dim + 1; |
|
77 if (sz (dim) != 1) |
|
78 plane = [plane, dim]; |
|
79 if (length (plane) == 2) |
|
80 break; |
|
81 endif |
|
82 endif |
|
83 endwhile |
|
84 if (length (plane) < 1) |
|
85 plane = [1, 2]; |
|
86 elseif (length (plane) < 2) |
|
87 plane = [1, plane]; |
|
88 endif |
|
89 else |
|
90 if (! (isvector (plane) && length (plane) == 2 |
|
91 && all (plane == round (plane)) && all (plane > 0) |
|
92 && all (plane < (nd + 1)) && plane(1) != plane(2))) |
|
93 error ("rotdim: plane must be a 2 element integer vector defining a valid plane"); |
|
94 endif |
|
95 endif |
|
96 |
|
97 k = rem (k, 4); |
|
98 if (k < 0) |
|
99 k = k + 4; |
|
100 endif |
|
101 if (k == 0) |
|
102 y = x; |
|
103 elseif (k == 2) |
5018
|
104 y = flipdim (flipdim (x, plane(1)), plane(2)); |
5012
|
105 elseif (k == 1 || k == 3) |
|
106 perm = 1:nd; |
|
107 perm(plane(1)) = plane(2); |
|
108 perm(plane(2)) = plane(1); |
|
109 y = permute (x, perm); |
|
110 if (k == 1) |
|
111 y = flipdim (y, min (plane)); |
|
112 else |
|
113 y = flipdim (y, max (plane)); |
|
114 endif |
|
115 else |
|
116 error ("rotdim: internal error!"); |
|
117 endif |
|
118 |
|
119 endfunction |