Mercurial > hg > octave-nkf
annotate scripts/general/rotdim.m @ 20441:83792dd9bcc1
Use in-place operators in m-files where possible.
* scripts/audio/@audioplayer/set.m, scripts/audio/@audiorecorder/set.m,
scripts/audio/mu2lin.m, scripts/elfun/cosd.m, scripts/general/del2.m,
scripts/general/profexplore.m, scripts/general/quadl.m, scripts/general/rat.m,
scripts/general/rotdim.m, scripts/help/get_first_help_sentence.m,
scripts/help/private/__strip_html_tags__.m, scripts/image/cubehelix.m,
scripts/io/textread.m, scripts/linear-algebra/duplication_matrix.m,
scripts/linear-algebra/housh.m, scripts/linear-algebra/krylov.m,
scripts/linear-algebra/logm.m, scripts/linear-algebra/normest.m,
scripts/linear-algebra/onenormest.m, scripts/optimization/fminsearch.m,
scripts/optimization/lsqnonneg.m, scripts/optimization/qp.m,
scripts/plot/appearance/annotation.m, scripts/plot/appearance/axis.m,
scripts/plot/appearance/legend.m, scripts/plot/appearance/specular.m,
scripts/plot/draw/colorbar.m, scripts/plot/draw/hist.m,
scripts/plot/draw/plotmatrix.m, scripts/plot/draw/private/__stem__.m,
scripts/plot/util/__actual_axis_position__.m,
scripts/plot/util/__gnuplot_drawnow__.m, scripts/plot/util/findobj.m,
scripts/plot/util/print.m, scripts/plot/util/private/__go_draw_axes__.m,
scripts/plot/util/private/__print_parse_opts__.m, scripts/plot/util/rotate.m,
scripts/polynomial/pchip.m, scripts/polynomial/polyaffine.m,
scripts/polynomial/polyder.m, scripts/polynomial/private/__splinefit__.m,
scripts/polynomial/residue.m, scripts/signal/arch_fit.m,
scripts/signal/arch_rnd.m, scripts/signal/bartlett.m,
scripts/signal/blackman.m, scripts/signal/freqz.m, scripts/signal/hamming.m,
scripts/signal/hanning.m, scripts/signal/spectral_adf.m,
scripts/signal/spectral_xdf.m, scripts/signal/stft.m,
scripts/sparse/bicgstab.m, scripts/sparse/cgs.m,
scripts/sparse/private/__sprand_impl__.m, scripts/sparse/qmr.m,
scripts/sparse/sprandsym.m, scripts/sparse/svds.m, scripts/specfun/legendre.m,
scripts/special-matrix/gallery.m, scripts/statistics/base/gls.m,
scripts/statistics/models/logistic_regression.m,
scripts/statistics/tests/kruskal_wallis_test.m,
scripts/statistics/tests/manova.m, scripts/statistics/tests/wilcoxon_test.m,
scripts/time/datevec.m:
Use in-place operators in m-files where possible.
author | Rik <rik@octave.org> |
---|---|
date | Tue, 26 May 2015 21:07:42 -0700 |
parents | 7503499a252b |
children |
rev | line source |
---|---|
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19358
diff
changeset
|
1 ## Copyright (C) 2004-2015 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. |
20368
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
25 ## |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
26 ## The second argument @var{n} is optional, and specifies how many 90-degree |
20368
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
27 ## rotations are to be applied (the default value is 1). Negative values of |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
28 ## @var{n} rotate the matrix in a clockwise direction. |
5012 | 29 ## |
20368
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
30 ## The third argument is also optional and defines the plane of the rotation. |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
31 ## If present, @var{plane} is a two element vector containing two different |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
32 ## valid dimensions of the matrix. When @var{plane} is not given the first two |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
33 ## non-singleton dimensions are used. |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
34 ## |
5012 | 35 ## For example, |
36 ## | |
37 ## @example | |
38 ## @group | |
39 ## rotdim ([1, 2; 3, 4], -1, [1, 2]) | |
40 ## @result{} 3 1 | |
41 ## 4 2 | |
42 ## @end group | |
43 ## @end example | |
44 ## | |
45 ## @noindent | |
46 ## rotates the given matrix clockwise by 90 degrees. The following are all | |
47 ## equivalent statements: | |
48 ## | |
49 ## @example | |
50 ## @group | |
5487 | 51 ## rotdim ([1, 2; 3, 4], -1, [1, 2]) |
52 ## rotdim ([1, 2; 3, 4], 3, [1, 2]) | |
53 ## rotdim ([1, 2; 3, 4], 7, [1, 2]) | |
5012 | 54 ## @end group |
55 ## @end example | |
19358
767103303974
doc: Re-order seealso references in rot90, rotdim.
Rik <rik@octave.org>
parents:
19318
diff
changeset
|
56 ## @seealso{rot90, fliplr, flipud, flip} |
5012 | 57 ## @end deftypefn |
58 | |
10689
6622772a0add
rotdim.m: Modify function to use same variable names as documentation
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
59 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
|
60 |
5012 | 61 if (nargin < 1 || nargin > 3) |
6046 | 62 print_usage (); |
5012 | 63 endif |
64 | |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
65 if (nargin > 1 && ! isempty (n)) |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
66 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
|
67 error ("rotdim: N must be a scalar integer"); |
5012 | 68 endif |
69 else | |
10689
6622772a0add
rotdim.m: Modify function to use same variable names as documentation
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
70 n = 1; |
5012 | 71 endif |
72 | |
73 nd = ndims (x); | |
74 sz = size (x); | |
75 if (nargin < 3) | |
12795
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
76 if (nd > 2) |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
77 ## 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
|
78 plane = []; |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
79 dim = 0; |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
80 while (dim < nd) |
20441
83792dd9bcc1
Use in-place operators in m-files where possible.
Rik <rik@octave.org>
parents:
20368
diff
changeset
|
81 dim += 1; |
12795
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
82 if (sz (dim) != 1) |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
83 plane = [plane, dim]; |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
84 if (length (plane) == 2) |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
85 break; |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
86 endif |
10549 | 87 endif |
12795
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
88 endwhile |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
89 if (length (plane) < 1) |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
90 plane = [1, 2]; |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
91 elseif (length (plane) < 2) |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
92 plane = [1, plane]; |
5012 | 93 endif |
12795
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
94 else |
5012 | 95 plane = [1, 2]; |
96 endif | |
97 else | |
98 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
|
99 && all (plane == fix (plane)) && all (plane > 0) |
10549 | 100 && 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
|
101 error ("rotdim: PLANE must be a 2 element integer vector defining a valid PLANE"); |
5012 | 102 endif |
103 endif | |
104 | |
10689
6622772a0add
rotdim.m: Modify function to use same variable names as documentation
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
105 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
|
106 if (n < 0) |
20441
83792dd9bcc1
Use in-place operators in m-files where possible.
Rik <rik@octave.org>
parents:
20368
diff
changeset
|
107 n += 4; |
5012 | 108 endif |
10689
6622772a0add
rotdim.m: Modify function to use same variable names as documentation
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
109 if (n == 0) |
5012 | 110 y = x; |
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 == 2) |
19318
995df67fc912
Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents:
19047
diff
changeset
|
112 y = flip (flip (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
|
113 elseif (n == 1 || n == 3) |
5012 | 114 perm = 1:nd; |
115 perm(plane(1)) = plane(2); | |
116 perm(plane(2)) = plane(1); | |
117 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
|
118 if (n == 1) |
19318
995df67fc912
Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents:
19047
diff
changeset
|
119 y = flip (y, min (plane)); |
5012 | 120 else |
19318
995df67fc912
Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents:
19047
diff
changeset
|
121 y = flip (y, max (plane)); |
5012 | 122 endif |
123 else | |
124 error ("rotdim: internal error!"); | |
125 endif | |
126 | |
127 endfunction | |
12795
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
128 |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
129 |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
130 %!shared r, rr |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
131 %! 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
|
132 %!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
|
133 %!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
|
134 %!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
|
135 %!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
|
136 %!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
|
137 %!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
|
138 |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
139 %!shared c, cr |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
140 %! 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
|
141 %!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
|
142 %!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
|
143 %!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
|
144 %!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
|
145 %!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
|
146 %!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
|
147 |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
148 %!shared m |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
149 %! 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
|
150 %!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
|
151 %!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
|
152 %!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
|
153 %!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
|
154 %!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
|
155 %!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
|
156 |
19047
7bbe3658c5ef
maint: Use "FIXME:" coding convention in m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
157 ## FIXME: We need tests for multidimensional arrays |
7bbe3658c5ef
maint: Use "FIXME:" coding convention in m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
158 ## and different values of PLANE. |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
159 |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
160 %!error rotdim () |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
161 %!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
|
162 |