Mercurial > octave-antonio
annotate scripts/general/repmat.m @ 8390:49901b624316
optimize repmat for scalar & matrix case
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Wed, 10 Dec 2008 11:04:28 +0100 |
parents | a1dbe9d80eee |
children | cadc73247d65 |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 2000, 2002, 2004, 2005, 2006, 2007 Paul Kienzle |
8390
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
2 ## Copyright (C) 2008 Jaroslav Hajek |
3914 | 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 | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
3914 | 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 | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
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) |
8390
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
48 ## Ensure that we have a row vector |
4844 | 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) |
8390
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
56 ## optimize the scalar fill case. |
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
57 x(1:prod (idx)) = a; |
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
58 x = reshape (x, idx); |
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
59 elseif (ndims (a) == 2 && length (idx) < 3) |
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
60 if (issparse (a)) |
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
61 x = spkron (ones (idx), a); |
4844 | 62 else |
8390
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
63 ## indexing is now faster, so we use it rather than kron. |
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
64 m = rows (a); n = columns (a); |
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
65 p = idx(1); q = idx(2); |
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
66 x = reshape (a, m, 1, n, 1); |
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
67 x = x(:, ones (1, p), :, ones (1, q)); |
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
68 x = reshape (x, m*p, n*q); |
4844 | 69 endif |
3914 | 70 else |
4844 | 71 aidx = size(a); |
72 if (length(aidx) > length(idx)) | |
73 idx = [idx, ones(1,length(aidx)-length(idx))]; | |
74 elseif (length(aidx) < length(idx)) | |
75 aidx = [aidx, ones(1,length(idx)-length(aidx))]; | |
76 endif | |
4945 | 77 cidx = cell (1, length (aidx)); |
4844 | 78 for i=1:length(aidx) |
79 cidx{i} = kron (ones (1, idx(i)), 1:aidx(i)); | |
80 endfor | |
81 x = a (cidx{:}); | |
3914 | 82 endif |
83 | |
84 endfunction | |
6987 | 85 |
86 # Test various methods of providing size parameters | |
87 %!shared x | |
88 %! x = [1 2;3 4]; | |
89 %!assert(repmat(x, [1 1]), repmat(x, 1)); | |
90 %!assert(repmat(x, [3 3]), repmat(x, 3)); | |
91 %!assert(repmat(x, [1 1]), repmat(x, 1, 1)); | |
92 %!assert(repmat(x, [1 3]), repmat(x, 1, 3)); | |
93 %!assert(repmat(x, [3 1]), repmat(x, 3, 1)); | |
94 %!assert(repmat(x, [3 3]), repmat(x, 3, 3)); | |
95 | |
96 # Tests for numel==1 case: | |
97 %!shared x, r | |
98 %! x = [ 65 ]; | |
99 %! r = kron(ones(2,2), x); | |
100 %!assert(r, repmat(x, [2 2])); | |
101 %!assert(char(r), repmat(char(x), [2 2])); | |
102 %!assert(int8(r), repmat(int8(x), [2 2])); | |
103 | |
104 # Tests for ndims==2 case: | |
105 %!shared x, r | |
106 %! x = [ 65 66 67 ]; | |
107 %! r = kron(ones(2,2), x); | |
108 %!assert(r, repmat(x, [2 2])); | |
109 %!assert(char(r), repmat(char(x), [2 2])); | |
110 %!assert(int8(r), repmat(int8(x), [2 2])); | |
111 | |
112 # Tests for dim>2 case: | |
113 %!shared x, r | |
114 %! x = [ 65 66 67 ]; | |
115 %! r = kron(ones(2,2), x); | |
116 %! r(:,:,2) = r(:,:,1); | |
117 %!assert(r, repmat(x, [2 2 2])); | |
118 %!assert(char(r), repmat(char(x), [2 2 2])); | |
119 %!assert(int8(r), repmat(int8(x), [2 2 2])); | |
120 | |
121 # Test that sparsity is kept | |
122 %!assert(sparse(4,4), repmat(sparse(2,2),[2 2])); | |
123 |