1 ## Copyright (C) 2004-2012 David Bateman
3 ## This file is part of Octave.
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 3 of the License, or (at
8 ## your option) any later version.
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.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {} rotdim (@var{x})
21 ## @deftypefnx {Function File} {} rotdim (@var{x}, @var{n})
22 ## @deftypefnx {Function File} {} rotdim (@var{x}, @var{n}, @var{plane})
23 ## Return a copy of @var{x} with the elements rotated counterclockwise in
24 ## 90-degree increments.
25 ## The second argument @var{n} is optional, and specifies how many 90-degree
26 ## rotations are to be applied (the default value is 1).
27 ## The third argument is also optional and defines the plane of the
28 ## rotation. If present, @var{plane} is a two element vector containing two
29 ## different valid dimensions of the matrix. When @var{plane} is not given
30 ## the first two non-singleton dimensions are used.
32 ## Negative values of @var{n} rotate the matrix in a clockwise direction.
37 ## rotdim ([1, 2; 3, 4], -1, [1, 2])
44 ## rotates the given matrix clockwise by 90 degrees. The following are all
45 ## equivalent statements:
49 ## rotdim ([1, 2; 3, 4], -1, [1, 2])
50 ## rotdim ([1, 2; 3, 4], 3, [1, 2])
51 ## rotdim ([1, 2; 3, 4], 7, [1, 2])
54 ## @seealso{rot90, flipud, fliplr, flipdim}
57 function y = rotdim (x, n, plane)
59 if (nargin < 1 || nargin > 3)
63 if (nargin > 1 && ! isempty(n))
64 if (!isscalar (n) || !isreal(n) || fix (n) != n)
65 error ("rotdim: N must be a scalar integer");
75 ## Find the first two non-singleton dimension.
82 if (length (plane) == 2)
87 if (length (plane) < 1)
89 elseif (length (plane) < 2)
96 if (! (isvector (plane) && length (plane) == 2
97 && all (plane == fix (plane)) && all (plane > 0)
98 && all (plane < (nd + 1)) && plane(1) != plane(2)))
99 error ("rotdim: PLANE must be a 2 element integer vector defining a valid PLANE");
110 y = flipdim (flipdim (x, plane(1)), plane(2));
111 elseif (n == 1 || n == 3)
113 perm(plane(1)) = plane(2);
114 perm(plane(2)) = plane(1);
115 y = permute (x, perm);
117 y = flipdim (y, min (plane));
119 y = flipdim (y, max (plane));
122 error ("rotdim: internal error!");
128 %!error rotdim (1, 2, 3, 4);
131 %! r = [1,2,3]; rr = [3,2,1];
132 %!assert (rotdim (r, 0), r);
133 %!assert (rotdim (r, 1), rr');
134 %!assert (rotdim (r, 2), rr);
135 %!assert (rotdim (r, 3), r');
136 %!assert (rotdim (r, 3), rotdim (r, -1));
137 %!assert (rotdim (r, 1), rotdim (r));
140 %! c = [1;2;3]; cr = [3;2;1];
141 %!assert (rotdim (c, 0), c);
142 %!assert (rotdim (c, 1), c');
143 %!assert (rotdim (c, 2), cr);
144 %!assert (rotdim (c, 3), cr');
145 %!assert (rotdim (c, 3), rotdim (c, -1));
146 %!assert (rotdim (c, 1), rotdim (c));
150 %!assert (rotdim (m, 0), m);
151 %!assert (rotdim (m, 1), [2,4;1,3]);
152 %!assert (rotdim (m, 2), [4,3;2,1]);
153 %!assert (rotdim (m, 3), [3,1;4,2]);
154 %!assert (rotdim (m, 3), rotdim (m, -1));
155 %!assert (rotdim (m, 1), rotdim (m));
157 ## FIXME -- we need tests for multidimensional arrays and different