1 ## Copyright (C) 2000-2012 Paul Kienzle
2 ## Copyright (C) 2008 Jaroslav Hajek
4 ## This file is part of Octave.
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
8 ## the Free Software Foundation; either version 3 of the License, or (at
9 ## your option) any later version.
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.
16 ## You should have received a copy of the GNU General Public License
17 ## along with Octave; see the file COPYING. If not, see
18 ## <http://www.gnu.org/licenses/>.
21 ## @deftypefn {Function File} {} repmat (@var{A}, @var{m})
22 ## @deftypefnx {Function File} {} repmat (@var{A}, @var{m}, @var{n})
23 ## @deftypefnx {Function File} {} repmat (@var{A}, @var{m}, @var{n}, @var{p}, @dots{})
24 ## @deftypefnx {Function File} {} repmat (@var{A}, [@var{m} @var{n}])
25 ## @deftypefnx {Function File} {} repmat (@var{A}, [@var{m} @var{n} @var{p} @dots{}])
26 ## Form a block matrix of size @var{m} by @var{n}, with a copy of matrix
27 ## @var{A} as each element. If @var{n} is not specified, form an
28 ## @var{m} by @var{m} block matrix.
32 ## Author: Paul Kienzle <pkienzle@kienzle.powernet.co.uk>
35 function x = repmat (A, m, n)
37 if (nargin < 2 || nargin > 3)
42 if (! (isscalar (m) && isscalar (n)))
43 error ("repmat: with 3 arguments M and N must be scalar");
50 elseif (isvector (m) && length (m) > 1)
51 ## Ensure that we have a row vector
54 error ("repmat: invalid dimensional argument");
59 error ("repmat: invalid dimensions");
65 ## optimize the scalar fill case.
72 elseif (ndims (A) == 2 && length (idx) < 3)
74 x = kron (ones (idx), A);
76 ## indexing is now faster, so we use it rather than kron.
77 m = rows (A); n = columns (A);
78 p = idx(1); q = idx(2);
79 x = reshape (A, m, 1, n, 1);
80 x = x(:, ones (1, p), :, ones (1, q));
81 x = reshape (x, m*p, n*q);
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));
90 for i = 1:length (aidx)
92 cidx{2,i} = ones (1, idx (i));
97 A = reshape (A, aaidx(:));
98 x = reshape (A (cidx{:}), idx .* aidx);
103 # Test various methods of providing size parameters
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));
113 # Tests for numel==1 case:
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]));
121 # Tests for ndims==2 case:
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]));
129 # Tests for dim>2 case:
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]));
138 # Test that sparsity is kept
139 %!assert(sparse(4,4), repmat(sparse(2,2),[2 2]));
142 %!assert (size (repmat (".", -1, 1)), [0, 1]);
143 %!assert (size (repmat (".", 1, -1)), [1, 0]);
144 %!error (size (repmat (".", -1, -1)));
146 %!assert (size (repmat (1, [1, 0])), [1, 0]);
147 %!assert (size (repmat (1, [5, 0])), [5, 0]);
148 %!assert (size (repmat (1, [0, 1])), [0, 1]);
149 %!assert (size (repmat (1, [0, 5])), [0, 5]);
152 %! x = struct ("a", [], "b", []);
153 %!assert (size (repmat (x, [1, 0])), [1, 0]);
154 %!assert (size (repmat (x, [5, 0])), [5, 0]);
155 %!assert (size (repmat (x, [0, 1])), [0, 1]);
156 %!assert (size (repmat (x, [0, 5])), [0, 5]);
158 %!assert (size (repmat ({1}, [1, 0])), [1, 0]);
159 %!assert (size (repmat ({1}, [5, 0])), [5, 0]);
160 %!assert (size (repmat ({1}, [0, 1])), [0, 1]);
161 %!assert (size (repmat ({1}, [0, 5])), [0, 5]);