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}]) |
|
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) |
|
34 usage ("repmat (a, m, n)"); |
|
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) |
|
55 if (isstr (a)) |
|
56 x = setstr (toascii (a) * ones (idx)); |
|
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) |
|
70 if (isstr (a)) |
|
71 x = setstr (kron (ones (idx), toascii (a))); |
4945
|
72 elseif (strcmp (class(a), "double")) |
|
73 x = kron (ones (idx), a); |
4844
|
74 else |
4945
|
75 aidx = size(a); |
|
76 x = a (kron (ones (1, idx(1)), 1:aidx(1)), |
|
77 kron (ones (1, idx(2)), 1:aidx(2))); |
4844
|
78 endif |
3914
|
79 else |
4844
|
80 aidx = size(a); |
|
81 if (length(aidx) > length(idx)) |
|
82 idx = [idx, ones(1,length(aidx)-length(idx))]; |
|
83 elseif (length(aidx) < length(idx)) |
|
84 aidx = [aidx, ones(1,length(idx)-length(aidx))]; |
|
85 endif |
4945
|
86 cidx = cell (1, length (aidx)); |
4844
|
87 for i=1:length(aidx) |
|
88 cidx{i} = kron (ones (1, idx(i)), 1:aidx(i)); |
|
89 endfor |
|
90 x = a (cidx{:}); |
3914
|
91 endif |
|
92 |
|
93 endfunction |