comparison scripts/general/rotdim.m @ 5012:ed25bed43409

[project @ 2004-09-21 15:45:48 by jwe]
author jwe
date Tue, 21 Sep 2004 15:45:48 +0000
parents
children 1c65a8e44ef9
comparison
equal deleted inserted replaced
5011:1743bf921773 5012:ed25bed43409
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
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
18 ## 02111-1307, USA.
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
55 ## @seealso{rot90, flipud, fliplr and flipdim}
56
57 function y = rotdim (x, k, plane)
58
59 if (nargin < 1 || nargin > 3)
60 usage ("rotdim (x, k, plane)");
61 endif
62
63 if (nargin > 1 && !isempty(k))
64 if (imag (k) != 0 || fix (k) != k)
65 error ("rotdim: k must be an integer");
66 endif
67 else
68 k = 1;
69 endif
70
71 nd = ndims (x);
72 sz = size (x);
73 if (nargin < 3)
74 ## Find the first two non-singleton dimension.
75 plane = [];
76 dim = 0;
77 while (dim < nd)
78 dim = dim + 1;
79 if (sz (dim) != 1)
80 plane = [plane, dim];
81 if (length (plane) == 2)
82 break;
83 endif
84 endif
85 endwhile
86 if (length (plane) < 1)
87 plane = [1, 2];
88 elseif (length (plane) < 2)
89 plane = [1, plane];
90 endif
91 else
92 if (! (isvector (plane) && length (plane) == 2
93 && all (plane == round (plane)) && all (plane > 0)
94 && all (plane < (nd + 1)) && plane(1) != plane(2)))
95 error ("rotdim: plane must be a 2 element integer vector defining a valid plane");
96 endif
97 endif
98
99 k = rem (k, 4);
100 if (k < 0)
101 k = k + 4;
102 endif
103 if (k == 0)
104 y = x;
105 elseif (k == 2)
106 y = flipdim (flipdim (x, plane (1)), plane (2));
107 elseif (k == 1 || k == 3)
108 perm = 1:nd;
109 perm(plane(1)) = plane(2);
110 perm(plane(2)) = plane(1);
111 y = permute (x, perm);
112 if (k == 1)
113 y = flipdim (y, min (plane));
114 else
115 y = flipdim (y, max (plane));
116 endif
117 else
118 error ("rotdim: internal error!");
119 endif
120
121 endfunction