Mercurial > hg > octave-nkf
annotate scripts/general/repmat.m @ 15434:e1f59fd57756
repmat() should interpret empties as implying unit.
Bug # 37390.
* scripts/general/repmat.m: If the inputs specifying the number to
replications is empty, interpret the empty as unity. Add tests.
author | Ben Abbott <bpabbott@mac.com> |
---|---|
date | Fri, 21 Sep 2012 21:13:33 -0400 |
parents | b76f0740940e |
children | 32fd31378052 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
1 ## Copyright (C) 2000-2012 Paul Kienzle |
8390
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
2 ## Copyright (C) 2008 Jaroslav Hajek |
3914 | 3 ## |
4 ## This file is part of Octave. | |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
3914 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
3914 | 19 |
20 ## -*- texinfo -*- | |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
21 ## @deftypefn {Function File} {} repmat (@var{A}, @var{m}) |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
22 ## @deftypefnx {Function File} {} repmat (@var{A}, @var{m}, @var{n}) |
3914 | 23 ## @deftypefnx {Function File} {} repmat (@var{A}, [@var{m} @var{n}]) |
9041
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
8970
diff
changeset
|
24 ## @deftypefnx {Function File} {} repmat (@var{A}, [@var{m} @var{n} @var{p} @dots{}]) |
3914 | 25 ## Form a block matrix of size @var{m} by @var{n}, with a copy of matrix |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
26 ## @var{A} as each element. If @var{n} is not specified, form an |
14366
b76f0740940e
doc: Periodic grammar check of documentation.
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
27 ## @var{m} by @var{m} block matrix. For copying along more than two |
14241
a52925666288
repmat.m: Fix incorrect docstring regarding higher dimensions
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
28 ## dimensions, specify the number of times to copy across each dimension |
a52925666288
repmat.m: Fix incorrect docstring regarding higher dimensions
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
29 ## @var{m}, @var{n}, @var{p}, @dots{}, in a vector in the second argument. |
10801
a40e32927b3a
Improve documentation for new repelems function.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
30 ## @seealso{repelems} |
3914 | 31 ## @end deftypefn |
32 | |
33 ## Author: Paul Kienzle <pkienzle@kienzle.powernet.co.uk> | |
34 ## Created: July 2000 | |
35 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11318
diff
changeset
|
36 function x = repmat (A, m, n) |
3915 | 37 |
3914 | 38 if (nargin < 2 || nargin > 3) |
6046 | 39 print_usage (); |
3914 | 40 endif |
41 | |
4844 | 42 if (nargin == 3) |
15434
e1f59fd57756
repmat() should interpret empties as implying unit.
Ben Abbott <bpabbott@mac.com>
parents:
14366
diff
changeset
|
43 if (! (numel (m) < 2 && numel (n) < 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:
11469
diff
changeset
|
44 error ("repmat: with 3 arguments M and N must be scalar"); |
4844 | 45 endif |
15434
e1f59fd57756
repmat() should interpret empties as implying unit.
Ben Abbott <bpabbott@mac.com>
parents:
14366
diff
changeset
|
46 if (isempty (m)) |
e1f59fd57756
repmat() should interpret empties as implying unit.
Ben Abbott <bpabbott@mac.com>
parents:
14366
diff
changeset
|
47 m = 1; |
e1f59fd57756
repmat() should interpret empties as implying unit.
Ben Abbott <bpabbott@mac.com>
parents:
14366
diff
changeset
|
48 endif |
e1f59fd57756
repmat() should interpret empties as implying unit.
Ben Abbott <bpabbott@mac.com>
parents:
14366
diff
changeset
|
49 if (isempty (n)) |
e1f59fd57756
repmat() should interpret empties as implying unit.
Ben Abbott <bpabbott@mac.com>
parents:
14366
diff
changeset
|
50 n = 1; |
e1f59fd57756
repmat() should interpret empties as implying unit.
Ben Abbott <bpabbott@mac.com>
parents:
14366
diff
changeset
|
51 endif |
4844 | 52 idx = [m, n]; |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
53 else |
15434
e1f59fd57756
repmat() should interpret empties as implying unit.
Ben Abbott <bpabbott@mac.com>
parents:
14366
diff
changeset
|
54 if (isempty (m)) |
e1f59fd57756
repmat() should interpret empties as implying unit.
Ben Abbott <bpabbott@mac.com>
parents:
14366
diff
changeset
|
55 m = 1; |
e1f59fd57756
repmat() should interpret empties as implying unit.
Ben Abbott <bpabbott@mac.com>
parents:
14366
diff
changeset
|
56 endif |
4030 | 57 if (isscalar (m)) |
4844 | 58 idx = [m, m]; |
3914 | 59 n = m; |
4844 | 60 elseif (isvector (m) && length (m) > 1) |
8390
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
61 ## Ensure that we have a row vector |
4844 | 62 idx = m(:).'; |
3914 | 63 else |
4844 | 64 error ("repmat: invalid dimensional argument"); |
3914 | 65 endif |
66 endif | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
67 |
8508
dee629f14bfa
repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
68 if (all (idx < 0)) |
dee629f14bfa
repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
69 error ("repmat: invalid dimensions"); |
dee629f14bfa
repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
70 else |
dee629f14bfa
repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
71 idx = max (idx, 0); |
dee629f14bfa
repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
72 endif |
3914 | 73 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11318
diff
changeset
|
74 if (numel (A) == 1) |
8390
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
75 ## optimize the scalar fill case. |
11318
d7ea780b036f
repmat: handle special case of replicating scalar using index vector containing zeros
John W. Eaton <jwe@octave.org>
parents:
10801
diff
changeset
|
76 if (any (idx == 0)) |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11318
diff
changeset
|
77 x = resize (A, idx); |
11318
d7ea780b036f
repmat: handle special case of replicating scalar using index vector containing zeros
John W. Eaton <jwe@octave.org>
parents:
10801
diff
changeset
|
78 else |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11318
diff
changeset
|
79 x(1:prod (idx)) = A; |
11318
d7ea780b036f
repmat: handle special case of replicating scalar using index vector containing zeros
John W. Eaton <jwe@octave.org>
parents:
10801
diff
changeset
|
80 x = reshape (x, idx); |
d7ea780b036f
repmat: handle special case of replicating scalar using index vector containing zeros
John W. Eaton <jwe@octave.org>
parents:
10801
diff
changeset
|
81 endif |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11318
diff
changeset
|
82 elseif (ndims (A) == 2 && length (idx) < 3) |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11318
diff
changeset
|
83 if (issparse (A)) |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11318
diff
changeset
|
84 x = kron (ones (idx), A); |
4844 | 85 else |
8390
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
86 ## indexing is now faster, so we use it rather than kron. |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11318
diff
changeset
|
87 m = rows (A); n = columns (A); |
8390
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
88 p = idx(1); q = idx(2); |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11318
diff
changeset
|
89 x = reshape (A, m, 1, n, 1); |
8390
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
90 x = x(:, ones (1, p), :, ones (1, q)); |
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
91 x = reshape (x, m*p, n*q); |
4844 | 92 endif |
3914 | 93 else |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11318
diff
changeset
|
94 aidx = size (A); |
8970 | 95 ## ensure matching size |
96 idx(end+1:length (aidx)) = 1; | |
97 aidx(end+1:length (idx)) = 1; | |
98 ## create subscript array | |
99 cidx = cell (2, length (aidx)); | |
8507 | 100 for i = 1:length (aidx) |
8970 | 101 cidx{1,i} = ':'; |
102 cidx{2,i} = ones (1, idx (i)); | |
4844 | 103 endfor |
8970 | 104 aaidx = aidx; |
105 # add singleton dims | |
106 aaidx(2,:) = 1; | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11318
diff
changeset
|
107 A = reshape (A, aaidx(:)); |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11318
diff
changeset
|
108 x = reshape (A (cidx{:}), idx .* aidx); |
3914 | 109 endif |
110 | |
111 endfunction | |
6987 | 112 |
113 # Test various methods of providing size parameters | |
114 %!shared x | |
115 %! x = [1 2;3 4]; | |
15434
e1f59fd57756
repmat() should interpret empties as implying unit.
Ben Abbott <bpabbott@mac.com>
parents:
14366
diff
changeset
|
116 %!assert (repmat (x, 1, []), repmat (x, 1)) |
e1f59fd57756
repmat() should interpret empties as implying unit.
Ben Abbott <bpabbott@mac.com>
parents:
14366
diff
changeset
|
117 %!assert (repmat (x, [], 3), repmat (x, [1, 3])) |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14241
diff
changeset
|
118 %!assert (repmat (x, [1 1]), repmat (x, 1)) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14241
diff
changeset
|
119 %!assert (repmat (x, [3 3]), repmat (x, 3)) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14241
diff
changeset
|
120 %!assert (repmat (x, [1 1]), repmat (x, 1, 1)) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14241
diff
changeset
|
121 %!assert (repmat (x, [1 3]), repmat (x, 1, 3)) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14241
diff
changeset
|
122 %!assert (repmat (x, [3 1]), repmat (x, 3, 1)) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14241
diff
changeset
|
123 %!assert (repmat (x, [3 3]), repmat (x, 3, 3)) |
6987 | 124 |
125 # Tests for numel==1 case: | |
126 %!shared x, r | |
127 %! x = [ 65 ]; | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14241
diff
changeset
|
128 %! r = kron (ones (2,2), x); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14241
diff
changeset
|
129 %!assert (r, repmat (x, [2 2])) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14241
diff
changeset
|
130 %!assert (char (r), repmat (char (x), [2 2])) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14241
diff
changeset
|
131 %!assert (int8 (r), repmat (int8 (x), [2 2])) |
6987 | 132 |
133 # Tests for ndims==2 case: | |
134 %!shared x, r | |
135 %! x = [ 65 66 67 ]; | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14241
diff
changeset
|
136 %! r = kron (ones (2,2), x); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14241
diff
changeset
|
137 %!assert (r, repmat (x, [2 2])) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14241
diff
changeset
|
138 %!assert (char (r), repmat (char (x), [2 2])) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14241
diff
changeset
|
139 %!assert (int8 (r), repmat (int8 (x), [2 2])) |
6987 | 140 |
141 # Tests for dim>2 case: | |
142 %!shared x, r | |
143 %! x = [ 65 66 67 ]; | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14241
diff
changeset
|
144 %! r = kron (ones (2,2), x); |
6987 | 145 %! r(:,:,2) = r(:,:,1); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14241
diff
changeset
|
146 %!assert (r, repmat (x, [2 2 2])) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14241
diff
changeset
|
147 %!assert (char (r), repmat (char (x), [2 2 2])) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14241
diff
changeset
|
148 %!assert (int8 (r), repmat (int8 (x), [2 2 2])) |
6987 | 149 |
150 # Test that sparsity is kept | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14241
diff
changeset
|
151 %!assert (sparse (4,4), repmat (sparse (2,2),[2 2])) |
6987 | 152 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14241
diff
changeset
|
153 %!assert (size (repmat (".", -1, 1)), [0, 1]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14241
diff
changeset
|
154 %!assert (size (repmat (".", 1, -1)), [1, 0]) |
8508
dee629f14bfa
repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
155 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14241
diff
changeset
|
156 %!assert (size (repmat (1, [1, 0])), [1, 0]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14241
diff
changeset
|
157 %!assert (size (repmat (1, [5, 0])), [5, 0]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14241
diff
changeset
|
158 %!assert (size (repmat (1, [0, 1])), [0, 1]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14241
diff
changeset
|
159 %!assert (size (repmat (1, [0, 5])), [0, 5]) |
11318
d7ea780b036f
repmat: handle special case of replicating scalar using index vector containing zeros
John W. Eaton <jwe@octave.org>
parents:
10801
diff
changeset
|
160 |
d7ea780b036f
repmat: handle special case of replicating scalar using index vector containing zeros
John W. Eaton <jwe@octave.org>
parents:
10801
diff
changeset
|
161 %!shared x |
d7ea780b036f
repmat: handle special case of replicating scalar using index vector containing zeros
John W. Eaton <jwe@octave.org>
parents:
10801
diff
changeset
|
162 %! x = struct ("a", [], "b", []); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14241
diff
changeset
|
163 %!assert (size (repmat (x, [1, 0])), [1, 0]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14241
diff
changeset
|
164 %!assert (size (repmat (x, [5, 0])), [5, 0]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14241
diff
changeset
|
165 %!assert (size (repmat (x, [0, 1])), [0, 1]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14241
diff
changeset
|
166 %!assert (size (repmat (x, [0, 5])), [0, 5]) |
11318
d7ea780b036f
repmat: handle special case of replicating scalar using index vector containing zeros
John W. Eaton <jwe@octave.org>
parents:
10801
diff
changeset
|
167 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14241
diff
changeset
|
168 %!assert (size (repmat ({1}, [1, 0])), [1, 0]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14241
diff
changeset
|
169 %!assert (size (repmat ({1}, [5, 0])), [5, 0]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14241
diff
changeset
|
170 %!assert (size (repmat ({1}, [0, 1])), [0, 1]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14241
diff
changeset
|
171 %!assert (size (repmat ({1}, [0, 5])), [0, 5]) |
11318
d7ea780b036f
repmat: handle special case of replicating scalar using index vector containing zeros
John W. Eaton <jwe@octave.org>
parents:
10801
diff
changeset
|
172 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14241
diff
changeset
|
173 %!error (size (repmat (".", -1, -1))) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14241
diff
changeset
|
174 |