]> Creatis software - CreaPhase.git/blob - octave_packages/m/general/repmat.m
update packages
[CreaPhase.git] / octave_packages / m / general / repmat.m
1 ## Copyright (C) 2000-2012 Paul Kienzle
2 ## Copyright (C) 2008 Jaroslav Hajek
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
8 ## the Free Software Foundation; either version 3 of the License, or (at
9 ## your option) any later version.
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
17 ## along with Octave; see the file COPYING.  If not, see
18 ## <http://www.gnu.org/licenses/>.
19
20 ## -*- texinfo -*-
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.
29 ## @seealso{repelems}
30 ## @end deftypefn
31
32 ## Author: Paul Kienzle <pkienzle@kienzle.powernet.co.uk>
33 ## Created: July 2000
34
35 function x = repmat (A, m, n)
36
37   if (nargin < 2 || nargin > 3)
38     print_usage ();
39   endif
40
41   if (nargin == 3)
42     if (! (isscalar (m) && isscalar (n)))
43       error ("repmat: with 3 arguments M and N must be scalar");
44     endif
45     idx = [m, n];
46   else
47     if (isscalar (m))
48       idx = [m, m];
49       n = m;
50     elseif (isvector (m) && length (m) > 1)
51       ## Ensure that we have a row vector
52       idx = m(:).';
53     else
54       error ("repmat: invalid dimensional argument");
55     endif
56   endif
57
58   if (all (idx < 0))
59     error ("repmat: invalid dimensions");
60   else
61     idx = max (idx, 0);
62   endif
63
64   if (numel (A) == 1)
65     ## optimize the scalar fill case.
66     if (any (idx == 0))
67       x = resize (A, idx);
68     else
69       x(1:prod (idx)) = A;
70       x = reshape (x, idx);
71     endif
72   elseif (ndims (A) == 2 && length (idx) < 3)
73     if (issparse (A))
74       x = kron (ones (idx), A);
75     else
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);
82     endif
83   else
84     aidx = size (A);
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)
91       cidx{1,i} = ':';
92       cidx{2,i} = ones (1, idx (i));
93     endfor
94     aaidx = aidx;
95     # add singleton dims
96     aaidx(2,:) = 1;
97     A = reshape (A, aaidx(:));
98     x = reshape (A (cidx{:}), idx .* aidx);
99   endif
100
101 endfunction
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
141
142 %!assert (size (repmat (".", -1, 1)), [0, 1]);
143 %!assert (size (repmat (".", 1, -1)), [1, 0]);
144 %!error (size (repmat (".", -1, -1)));
145
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]);
150
151 %!shared x
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]);
157
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]);
162