Mercurial > octave-antonio
annotate scripts/general/repmat.m @ 8920:eb63fbe60fab
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sat, 07 Mar 2009 10:41:27 -0500 |
parents | dee629f14bfa |
children | b37a6c27c23f |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2000, 2002, 2004, 2005, 2006, 2007, 2009 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 | |
8508
dee629f14bfa
repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
54 |
dee629f14bfa
repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
55 if (all (idx < 0)) |
dee629f14bfa
repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
56 error ("repmat: invalid dimensions"); |
dee629f14bfa
repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
57 else |
dee629f14bfa
repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
58 idx = max (idx, 0); |
dee629f14bfa
repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
59 endif |
3914 | 60 |
4844 | 61 if (numel (a) == 1) |
8390
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
62 ## optimize the scalar fill case. |
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
63 x(1:prod (idx)) = a; |
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
64 x = reshape (x, idx); |
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
65 elseif (ndims (a) == 2 && length (idx) < 3) |
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
66 if (issparse (a)) |
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
67 x = spkron (ones (idx), a); |
4844 | 68 else |
8390
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
69 ## 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
|
70 m = rows (a); n = columns (a); |
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
71 p = idx(1); q = idx(2); |
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
72 x = reshape (a, m, 1, n, 1); |
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
73 x = x(:, ones (1, p), :, ones (1, q)); |
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
74 x = reshape (x, m*p, n*q); |
4844 | 75 endif |
3914 | 76 else |
4844 | 77 aidx = size(a); |
78 if (length(aidx) > length(idx)) | |
79 idx = [idx, ones(1,length(aidx)-length(idx))]; | |
80 elseif (length(aidx) < length(idx)) | |
81 aidx = [aidx, ones(1,length(idx)-length(aidx))]; | |
82 endif | |
4945 | 83 cidx = cell (1, length (aidx)); |
8507 | 84 for i = 1:length (aidx) |
4844 | 85 cidx{i} = kron (ones (1, idx(i)), 1:aidx(i)); |
86 endfor | |
87 x = a (cidx{:}); | |
3914 | 88 endif |
89 | |
90 endfunction | |
6987 | 91 |
92 # Test various methods of providing size parameters | |
93 %!shared x | |
94 %! x = [1 2;3 4]; | |
95 %!assert(repmat(x, [1 1]), repmat(x, 1)); | |
96 %!assert(repmat(x, [3 3]), repmat(x, 3)); | |
97 %!assert(repmat(x, [1 1]), repmat(x, 1, 1)); | |
98 %!assert(repmat(x, [1 3]), repmat(x, 1, 3)); | |
99 %!assert(repmat(x, [3 1]), repmat(x, 3, 1)); | |
100 %!assert(repmat(x, [3 3]), repmat(x, 3, 3)); | |
101 | |
102 # Tests for numel==1 case: | |
103 %!shared x, r | |
104 %! x = [ 65 ]; | |
105 %! r = kron(ones(2,2), x); | |
106 %!assert(r, repmat(x, [2 2])); | |
107 %!assert(char(r), repmat(char(x), [2 2])); | |
108 %!assert(int8(r), repmat(int8(x), [2 2])); | |
109 | |
110 # Tests for ndims==2 case: | |
111 %!shared x, r | |
112 %! x = [ 65 66 67 ]; | |
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 dim>2 case: | |
119 %!shared x, r | |
120 %! x = [ 65 66 67 ]; | |
121 %! r = kron(ones(2,2), x); | |
122 %! r(:,:,2) = r(:,:,1); | |
123 %!assert(r, repmat(x, [2 2 2])); | |
124 %!assert(char(r), repmat(char(x), [2 2 2])); | |
125 %!assert(int8(r), repmat(int8(x), [2 2 2])); | |
126 | |
127 # Test that sparsity is kept | |
128 %!assert(sparse(4,4), repmat(sparse(2,2),[2 2])); | |
129 | |
8508
dee629f14bfa
repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
130 |
dee629f14bfa
repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
131 %!assert (size (repmat (".", -1, 1)), [0, 1]); |
dee629f14bfa
repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
132 %!assert (size (repmat (".", 1, -1)), [1, 0]); |
dee629f14bfa
repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
133 %!error (size (repmat (".", -1, -1))); |