Mercurial > hg > octave-nkf
annotate scripts/general/repmat.m @ 14138:72c96de7a403 stable
maint: update copyright notices for 2012
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 02 Jan 2012 14:25:41 -0500 |
parents | 4d777e05d47c |
children | a52925666288 |
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}) |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
23 ## @deftypefnx {Function File} {} repmat (@var{A}, @var{m}, @var{n}, @var{p}, @dots{}) |
3914 | 24 ## @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
|
25 ## @deftypefnx {Function File} {} repmat (@var{A}, [@var{m} @var{n} @var{p} @dots{}]) |
3914 | 26 ## 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
|
27 ## @var{A} as each element. If @var{n} is not specified, form an |
3914 | 28 ## @var{m} by @var{m} block matrix. |
10801
a40e32927b3a
Improve documentation for new repelems function.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
29 ## @seealso{repelems} |
3914 | 30 ## @end deftypefn |
31 | |
32 ## Author: Paul Kienzle <pkienzle@kienzle.powernet.co.uk> | |
33 ## Created: July 2000 | |
34 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11318
diff
changeset
|
35 function x = repmat (A, m, n) |
3915 | 36 |
3914 | 37 if (nargin < 2 || nargin > 3) |
6046 | 38 print_usage (); |
3914 | 39 endif |
40 | |
4844 | 41 if (nargin == 3) |
5195 | 42 if (! (isscalar (m) && isscalar (n))) |
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
|
43 error ("repmat: with 3 arguments M and N must be scalar"); |
4844 | 44 endif |
45 idx = [m, n]; | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
46 else |
4030 | 47 if (isscalar (m)) |
4844 | 48 idx = [m, m]; |
3914 | 49 n = m; |
4844 | 50 elseif (isvector (m) && length (m) > 1) |
8390
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
51 ## Ensure that we have a row vector |
4844 | 52 idx = m(:).'; |
3914 | 53 else |
4844 | 54 error ("repmat: invalid dimensional argument"); |
3914 | 55 endif |
56 endif | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
57 |
8508
dee629f14bfa
repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
58 if (all (idx < 0)) |
dee629f14bfa
repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
59 error ("repmat: invalid dimensions"); |
dee629f14bfa
repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
60 else |
dee629f14bfa
repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
61 idx = max (idx, 0); |
dee629f14bfa
repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
62 endif |
3914 | 63 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11318
diff
changeset
|
64 if (numel (A) == 1) |
8390
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
65 ## 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
|
66 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
|
67 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
|
68 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
|
69 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
|
70 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
|
71 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
|
72 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
|
73 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
|
74 x = kron (ones (idx), A); |
4844 | 75 else |
8390
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
76 ## 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
|
77 m = rows (A); n = columns (A); |
8390
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
78 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
|
79 x = reshape (A, m, 1, n, 1); |
8390
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
80 x = x(:, ones (1, p), :, ones (1, q)); |
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
81 x = reshape (x, m*p, n*q); |
4844 | 82 endif |
3914 | 83 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
|
84 aidx = size (A); |
8970 | 85 ## ensure matching size |
86 idx(end+1:length (aidx)) = 1; | |
87 aidx(end+1:length (idx)) = 1; | |
88 ## create subscript array | |
89 cidx = cell (2, length (aidx)); | |
8507 | 90 for i = 1:length (aidx) |
8970 | 91 cidx{1,i} = ':'; |
92 cidx{2,i} = ones (1, idx (i)); | |
4844 | 93 endfor |
8970 | 94 aaidx = aidx; |
95 # add singleton dims | |
96 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
|
97 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
|
98 x = reshape (A (cidx{:}), idx .* aidx); |
3914 | 99 endif |
100 | |
101 endfunction | |
6987 | 102 |
103 # Test various methods of providing size parameters | |
104 %!shared x | |
105 %! x = [1 2;3 4]; | |
106 %!assert(repmat(x, [1 1]), repmat(x, 1)); | |
107 %!assert(repmat(x, [3 3]), repmat(x, 3)); | |
108 %!assert(repmat(x, [1 1]), repmat(x, 1, 1)); | |
109 %!assert(repmat(x, [1 3]), repmat(x, 1, 3)); | |
110 %!assert(repmat(x, [3 1]), repmat(x, 3, 1)); | |
111 %!assert(repmat(x, [3 3]), repmat(x, 3, 3)); | |
112 | |
113 # Tests for numel==1 case: | |
114 %!shared x, r | |
115 %! x = [ 65 ]; | |
116 %! r = kron(ones(2,2), x); | |
117 %!assert(r, repmat(x, [2 2])); | |
118 %!assert(char(r), repmat(char(x), [2 2])); | |
119 %!assert(int8(r), repmat(int8(x), [2 2])); | |
120 | |
121 # Tests for ndims==2 case: | |
122 %!shared x, r | |
123 %! x = [ 65 66 67 ]; | |
124 %! r = kron(ones(2,2), x); | |
125 %!assert(r, repmat(x, [2 2])); | |
126 %!assert(char(r), repmat(char(x), [2 2])); | |
127 %!assert(int8(r), repmat(int8(x), [2 2])); | |
128 | |
129 # Tests for dim>2 case: | |
130 %!shared x, r | |
131 %! x = [ 65 66 67 ]; | |
132 %! r = kron(ones(2,2), x); | |
133 %! r(:,:,2) = r(:,:,1); | |
134 %!assert(r, repmat(x, [2 2 2])); | |
135 %!assert(char(r), repmat(char(x), [2 2 2])); | |
136 %!assert(int8(r), repmat(int8(x), [2 2 2])); | |
137 | |
138 # Test that sparsity is kept | |
139 %!assert(sparse(4,4), repmat(sparse(2,2),[2 2])); | |
140 | |
8508
dee629f14bfa
repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
141 |
dee629f14bfa
repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
142 %!assert (size (repmat (".", -1, 1)), [0, 1]); |
dee629f14bfa
repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
143 %!assert (size (repmat (".", 1, -1)), [1, 0]); |
dee629f14bfa
repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
144 %!error (size (repmat (".", -1, -1))); |
11318
d7ea780b036f
repmat: handle special case of replicating scalar using index vector containing zeros
John W. Eaton <jwe@octave.org>
parents:
10801
diff
changeset
|
145 |
d7ea780b036f
repmat: handle special case of replicating scalar using index vector containing zeros
John W. Eaton <jwe@octave.org>
parents:
10801
diff
changeset
|
146 %!assert (size (repmat (1, [1, 0])), [1, 0]); |
d7ea780b036f
repmat: handle special case of replicating scalar using index vector containing zeros
John W. Eaton <jwe@octave.org>
parents:
10801
diff
changeset
|
147 %!assert (size (repmat (1, [5, 0])), [5, 0]); |
d7ea780b036f
repmat: handle special case of replicating scalar using index vector containing zeros
John W. Eaton <jwe@octave.org>
parents:
10801
diff
changeset
|
148 %!assert (size (repmat (1, [0, 1])), [0, 1]); |
d7ea780b036f
repmat: handle special case of replicating scalar using index vector containing zeros
John W. Eaton <jwe@octave.org>
parents:
10801
diff
changeset
|
149 %!assert (size (repmat (1, [0, 5])), [0, 5]); |
d7ea780b036f
repmat: handle special case of replicating scalar using index vector containing zeros
John W. Eaton <jwe@octave.org>
parents:
10801
diff
changeset
|
150 |
d7ea780b036f
repmat: handle special case of replicating scalar using index vector containing zeros
John W. Eaton <jwe@octave.org>
parents:
10801
diff
changeset
|
151 %!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
|
152 %! x = struct ("a", [], "b", []); |
d7ea780b036f
repmat: handle special case of replicating scalar using index vector containing zeros
John W. Eaton <jwe@octave.org>
parents:
10801
diff
changeset
|
153 %!assert (size (repmat (x, [1, 0])), [1, 0]); |
d7ea780b036f
repmat: handle special case of replicating scalar using index vector containing zeros
John W. Eaton <jwe@octave.org>
parents:
10801
diff
changeset
|
154 %!assert (size (repmat (x, [5, 0])), [5, 0]); |
d7ea780b036f
repmat: handle special case of replicating scalar using index vector containing zeros
John W. Eaton <jwe@octave.org>
parents:
10801
diff
changeset
|
155 %!assert (size (repmat (x, [0, 1])), [0, 1]); |
d7ea780b036f
repmat: handle special case of replicating scalar using index vector containing zeros
John W. Eaton <jwe@octave.org>
parents:
10801
diff
changeset
|
156 %!assert (size (repmat (x, [0, 5])), [0, 5]); |
d7ea780b036f
repmat: handle special case of replicating scalar using index vector containing zeros
John W. Eaton <jwe@octave.org>
parents:
10801
diff
changeset
|
157 |
d7ea780b036f
repmat: handle special case of replicating scalar using index vector containing zeros
John W. Eaton <jwe@octave.org>
parents:
10801
diff
changeset
|
158 %!assert (size (repmat ({1}, [1, 0])), [1, 0]); |
d7ea780b036f
repmat: handle special case of replicating scalar using index vector containing zeros
John W. Eaton <jwe@octave.org>
parents:
10801
diff
changeset
|
159 %!assert (size (repmat ({1}, [5, 0])), [5, 0]); |
d7ea780b036f
repmat: handle special case of replicating scalar using index vector containing zeros
John W. Eaton <jwe@octave.org>
parents:
10801
diff
changeset
|
160 %!assert (size (repmat ({1}, [0, 1])), [0, 1]); |
d7ea780b036f
repmat: handle special case of replicating scalar using index vector containing zeros
John W. Eaton <jwe@octave.org>
parents:
10801
diff
changeset
|
161 %!assert (size (repmat ({1}, [0, 5])), [0, 5]); |
d7ea780b036f
repmat: handle special case of replicating scalar using index vector containing zeros
John W. Eaton <jwe@octave.org>
parents:
10801
diff
changeset
|
162 |