Mercurial > hg > octave-nkf
annotate scripts/general/rotdim.m @ 15538:94d21131fefd
maint: periodic merge of stable to default
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 17 Oct 2012 11:51:35 -0400 |
parents | 5d3a684236b0 |
children | d63878346099 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
13279
diff
changeset
|
1 ## Copyright (C) 2004-2012 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 -*- | |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
20 ## @deftypefn {Function File} {} rotdim (@var{x}) |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
21 ## @deftypefnx {Function File} {} rotdim (@var{x}, @var{n}) |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
22 ## @deftypefnx {Function File} {} rotdim (@var{x}, @var{n}, @var{plane}) |
5012 | 23 ## Return a copy of @var{x} with the elements rotated counterclockwise in |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
24 ## 90-degree increments. |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
25 ## The second argument @var{n} is optional, and specifies how many 90-degree |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
26 ## rotations are to be applied (the default value is 1). |
5012 | 27 ## The third argument is also optional and defines the plane of the |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
28 ## rotation. If present, @var{plane} is a two element vector containing two |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
29 ## different valid dimensions of the matrix. When @var{plane} is not given |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
30 ## the first two non-singleton dimensions are used. |
5012 | 31 ## |
32 ## Negative values of @var{n} rotate the matrix in a clockwise direction. | |
33 ## For example, | |
34 ## | |
35 ## @example | |
36 ## @group | |
37 ## rotdim ([1, 2; 3, 4], -1, [1, 2]) | |
38 ## @result{} 3 1 | |
39 ## 4 2 | |
40 ## @end group | |
41 ## @end example | |
42 ## | |
43 ## @noindent | |
44 ## rotates the given matrix clockwise by 90 degrees. The following are all | |
45 ## equivalent statements: | |
46 ## | |
47 ## @example | |
48 ## @group | |
5487 | 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]) | |
5012 | 52 ## @end group |
53 ## @end example | |
5642 | 54 ## @seealso{rot90, flipud, fliplr, flipdim} |
5012 | 55 ## @end deftypefn |
56 | |
10689
6622772a0add
rotdim.m: Modify function to use same variable names as documentation
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
57 function y = rotdim (x, n, plane) |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
58 |
5012 | 59 if (nargin < 1 || nargin > 3) |
6046 | 60 print_usage (); |
5012 | 61 endif |
62 | |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
63 if (nargin > 1 && ! isempty (n)) |
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
64 if (!isscalar (n) || !isreal (n) || fix (n) != n) |
10689
6622772a0add
rotdim.m: Modify function to use same variable names as documentation
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
65 error ("rotdim: N must be a scalar integer"); |
5012 | 66 endif |
67 else | |
10689
6622772a0add
rotdim.m: Modify function to use same variable names as documentation
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
68 n = 1; |
5012 | 69 endif |
70 | |
71 nd = ndims (x); | |
72 sz = size (x); | |
73 if (nargin < 3) | |
12795
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
74 if (nd > 2) |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
75 ## Find the first two non-singleton dimension. |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
76 plane = []; |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
77 dim = 0; |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
78 while (dim < nd) |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
79 dim = dim + 1; |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
80 if (sz (dim) != 1) |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
81 plane = [plane, dim]; |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
82 if (length (plane) == 2) |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
83 break; |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
84 endif |
10549 | 85 endif |
12795
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
86 endwhile |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
87 if (length (plane) < 1) |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
88 plane = [1, 2]; |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
89 elseif (length (plane) < 2) |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
90 plane = [1, plane]; |
5012 | 91 endif |
12795
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
92 else |
5012 | 93 plane = [1, 2]; |
94 endif | |
95 else | |
96 if (! (isvector (plane) && length (plane) == 2 | |
13279
984359717d71
Use common code idiom for checking whether a double value is an integer.
Rik <octave@nomad.inbox5.com>
parents:
12795
diff
changeset
|
97 && all (plane == fix (plane)) && all (plane > 0) |
10549 | 98 && all (plane < (nd + 1)) && plane(1) != plane(2))) |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
10689
diff
changeset
|
99 error ("rotdim: PLANE must be a 2 element integer vector defining a valid PLANE"); |
5012 | 100 endif |
101 endif | |
102 | |
10689
6622772a0add
rotdim.m: Modify function to use same variable names as documentation
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
103 n = rem (n, 4); |
6622772a0add
rotdim.m: Modify function to use same variable names as documentation
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
104 if (n < 0) |
6622772a0add
rotdim.m: Modify function to use same variable names as documentation
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
105 n = n + 4; |
5012 | 106 endif |
10689
6622772a0add
rotdim.m: Modify function to use same variable names as documentation
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
107 if (n == 0) |
5012 | 108 y = x; |
10689
6622772a0add
rotdim.m: Modify function to use same variable names as documentation
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
109 elseif (n == 2) |
5018 | 110 y = flipdim (flipdim (x, plane(1)), plane(2)); |
10689
6622772a0add
rotdim.m: Modify function to use same variable names as documentation
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
111 elseif (n == 1 || n == 3) |
5012 | 112 perm = 1:nd; |
113 perm(plane(1)) = plane(2); | |
114 perm(plane(2)) = plane(1); | |
115 y = permute (x, perm); | |
10689
6622772a0add
rotdim.m: Modify function to use same variable names as documentation
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
116 if (n == 1) |
5012 | 117 y = flipdim (y, min (plane)); |
118 else | |
119 y = flipdim (y, max (plane)); | |
120 endif | |
121 else | |
122 error ("rotdim: internal error!"); | |
123 endif | |
124 | |
125 endfunction | |
12795
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
126 |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
127 |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
128 %!shared r, rr |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
129 %! r = [1,2,3]; rr = [3,2,1]; |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
130 %!assert (rotdim (r, 0), r) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
131 %!assert (rotdim (r, 1), rr') |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
132 %!assert (rotdim (r, 2), rr) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
133 %!assert (rotdim (r, 3), r') |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
134 %!assert (rotdim (r, 3), rotdim (r, -1)) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
135 %!assert (rotdim (r, 1), rotdim (r)) |
12795
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
136 |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
137 %!shared c, cr |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
138 %! c = [1;2;3]; cr = [3;2;1]; |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
139 %!assert (rotdim (c, 0), c) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
140 %!assert (rotdim (c, 1), c') |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
141 %!assert (rotdim (c, 2), cr) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
142 %!assert (rotdim (c, 3), cr') |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
143 %!assert (rotdim (c, 3), rotdim (c, -1)) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
144 %!assert (rotdim (c, 1), rotdim (c)) |
12795
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
145 |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
146 %!shared m |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
147 %! m = [1,2;3,4]; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
148 %!assert (rotdim (m, 0), m) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
149 %!assert (rotdim (m, 1), [2,4;1,3]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
150 %!assert (rotdim (m, 2), [4,3;2,1]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
151 %!assert (rotdim (m, 3), [3,1;4,2]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
152 %!assert (rotdim (m, 3), rotdim (m, -1)) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
153 %!assert (rotdim (m, 1), rotdim (m)) |
12795
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
154 |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
155 ## FIXME -- we need tests for multidimensional arrays and different |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
156 ## values of PLANE. |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
157 |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
158 %!error rotdim () |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
159 %!error rotdim (1, 2, 3, 4) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
160 |