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