7017
|
1 ## Copyright (C) 2000, 2002, 2004, 2005, 2006, 2007 Paul Kienzle |
3914
|
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. |
3914
|
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/>. |
3914
|
18 |
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {} repmat (@var{A}, @var{m}, @var{n}) |
|
21 ## @deftypefnx {Function File} {} repmat (@var{A}, [@var{m} @var{n}]) |
6987
|
22 ## @deftypefnx {Function File} {} repmat (@var{A}, [@var{m} @var{n} @var{p} ...]) |
3914
|
23 ## Form a block matrix of size @var{m} by @var{n}, with a copy of matrix |
|
24 ## @var{A} as each element. If @var{n} is not specified, form an |
|
25 ## @var{m} by @var{m} block matrix. |
|
26 ## @end deftypefn |
|
27 |
|
28 ## Author: Paul Kienzle <pkienzle@kienzle.powernet.co.uk> |
|
29 ## Created: July 2000 |
|
30 |
3915
|
31 function x = repmat (a, m, n) |
|
32 |
3914
|
33 if (nargin < 2 || nargin > 3) |
6046
|
34 print_usage (); |
3914
|
35 endif |
|
36 |
4844
|
37 if (nargin == 3) |
5195
|
38 if (! (isscalar (m) && isscalar (n))) |
4844
|
39 error ("repmat: with 3 arguments m and n must be scalar"); |
|
40 endif |
|
41 idx = [m, n]; |
|
42 else |
4030
|
43 if (isscalar (m)) |
4844
|
44 idx = [m, m]; |
3914
|
45 n = m; |
4844
|
46 elseif (isvector (m) && length (m) > 1) |
|
47 # Ensure that we have a row vector |
|
48 idx = m(:).'; |
3914
|
49 else |
4844
|
50 error ("repmat: invalid dimensional argument"); |
3914
|
51 endif |
|
52 endif |
|
53 |
4844
|
54 if (numel (a) == 1) |
5443
|
55 if (ischar (a)) |
|
56 x = char (toascii (a) * ones (idx)); |
4844
|
57 else |
4964
|
58 if (strcmp (class (a), "double")) |
|
59 ## This is faster with octave for double/Complex |
|
60 x = a * ones(idx, class(a)); |
|
61 else |
|
62 cidx = cell (1, length (idx)); |
|
63 for i=1:length(idx) |
|
64 cidx{i} = ones (1,idx(i)); |
|
65 endfor |
|
66 x = a (cidx{:}); |
|
67 endif |
4844
|
68 endif |
|
69 elseif (ndims (a) == 2 && length (idx) < 3) |
5443
|
70 if (ischar (a)) |
|
71 x = char (kron (ones (idx), toascii (a))); |
6987
|
72 elseif (strcmp (class(a), "double")) |
|
73 ## FIXME -- DISPATCH. |
|
74 if (issparse (a)) |
|
75 x = spkron (ones (idx), a); |
|
76 else |
|
77 x = kron (ones (idx), a); |
|
78 endif |
4844
|
79 else |
4945
|
80 aidx = size(a); |
|
81 x = a (kron (ones (1, idx(1)), 1:aidx(1)), |
|
82 kron (ones (1, idx(2)), 1:aidx(2))); |
4844
|
83 endif |
3914
|
84 else |
4844
|
85 aidx = size(a); |
|
86 if (length(aidx) > length(idx)) |
|
87 idx = [idx, ones(1,length(aidx)-length(idx))]; |
|
88 elseif (length(aidx) < length(idx)) |
|
89 aidx = [aidx, ones(1,length(idx)-length(aidx))]; |
|
90 endif |
4945
|
91 cidx = cell (1, length (aidx)); |
4844
|
92 for i=1:length(aidx) |
|
93 cidx{i} = kron (ones (1, idx(i)), 1:aidx(i)); |
|
94 endfor |
|
95 x = a (cidx{:}); |
3914
|
96 endif |
|
97 |
|
98 endfunction |
6987
|
99 |
|
100 # Test various methods of providing size parameters |
|
101 %!shared x |
|
102 %! x = [1 2;3 4]; |
|
103 %!assert(repmat(x, [1 1]), repmat(x, 1)); |
|
104 %!assert(repmat(x, [3 3]), repmat(x, 3)); |
|
105 %!assert(repmat(x, [1 1]), repmat(x, 1, 1)); |
|
106 %!assert(repmat(x, [1 3]), repmat(x, 1, 3)); |
|
107 %!assert(repmat(x, [3 1]), repmat(x, 3, 1)); |
|
108 %!assert(repmat(x, [3 3]), repmat(x, 3, 3)); |
|
109 |
|
110 # Tests for numel==1 case: |
|
111 %!shared x, r |
|
112 %! x = [ 65 ]; |
|
113 %! r = kron(ones(2,2), x); |
|
114 %!assert(r, repmat(x, [2 2])); |
|
115 %!assert(char(r), repmat(char(x), [2 2])); |
|
116 %!assert(int8(r), repmat(int8(x), [2 2])); |
|
117 |
|
118 # Tests for ndims==2 case: |
|
119 %!shared x, r |
|
120 %! x = [ 65 66 67 ]; |
|
121 %! r = kron(ones(2,2), x); |
|
122 %!assert(r, repmat(x, [2 2])); |
|
123 %!assert(char(r), repmat(char(x), [2 2])); |
|
124 %!assert(int8(r), repmat(int8(x), [2 2])); |
|
125 |
|
126 # Tests for dim>2 case: |
|
127 %!shared x, r |
|
128 %! x = [ 65 66 67 ]; |
|
129 %! r = kron(ones(2,2), x); |
|
130 %! r(:,:,2) = r(:,:,1); |
|
131 %!assert(r, repmat(x, [2 2 2])); |
|
132 %!assert(char(r), repmat(char(x), [2 2 2])); |
|
133 %!assert(int8(r), repmat(int8(x), [2 2 2])); |
|
134 |
|
135 # Test that sparsity is kept |
|
136 %!assert(sparse(4,4), repmat(sparse(2,2),[2 2])); |
|
137 |