annotate scripts/general/repmat.m @ 11032:c9b0a75b02e8

Make all regexp in Octave compatible with both POSIX and PCRE.
author Rik <octave@nomad.inbox5.com>
date Tue, 28 Sep 2010 09:25:14 -0700
parents a40e32927b3a
children d7ea780b036f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8920
eb63fbe60fab update copyright notices
John W. Eaton <jwe@octave.org>
parents: 8508
diff changeset
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
9eb96199e0f1 [project @ 2002-04-30 02:40:04 by jwe]
jwe
parents:
diff changeset
3 ##
9eb96199e0f1 [project @ 2002-04-30 02:40:04 by jwe]
jwe
parents:
diff changeset
4 ## This file is part of Octave.
9eb96199e0f1 [project @ 2002-04-30 02:40:04 by jwe]
jwe
parents:
diff changeset
5 ##
9eb96199e0f1 [project @ 2002-04-30 02:40:04 by jwe]
jwe
parents:
diff changeset
6 ## Octave is free software; you can redistribute it and/or modify it
9eb96199e0f1 [project @ 2002-04-30 02:40:04 by jwe]
jwe
parents:
diff changeset
7 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6987
diff changeset
8 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6987
diff changeset
9 ## your option) any later version.
3914
9eb96199e0f1 [project @ 2002-04-30 02:40:04 by jwe]
jwe
parents:
diff changeset
10 ##
9eb96199e0f1 [project @ 2002-04-30 02:40:04 by jwe]
jwe
parents:
diff changeset
11 ## Octave is distributed in the hope that it will be useful, but
9eb96199e0f1 [project @ 2002-04-30 02:40:04 by jwe]
jwe
parents:
diff changeset
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
9eb96199e0f1 [project @ 2002-04-30 02:40:04 by jwe]
jwe
parents:
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9eb96199e0f1 [project @ 2002-04-30 02:40:04 by jwe]
jwe
parents:
diff changeset
14 ## General Public License for more details.
9eb96199e0f1 [project @ 2002-04-30 02:40:04 by jwe]
jwe
parents:
diff changeset
15 ##
9eb96199e0f1 [project @ 2002-04-30 02:40:04 by jwe]
jwe
parents:
diff changeset
16 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6987
diff changeset
17 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6987
diff changeset
18 ## <http://www.gnu.org/licenses/>.
3914
9eb96199e0f1 [project @ 2002-04-30 02:40:04 by jwe]
jwe
parents:
diff changeset
19
9eb96199e0f1 [project @ 2002-04-30 02:40:04 by jwe]
jwe
parents:
diff changeset
20 ## -*- texinfo -*-
10793
be55736a0783 Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents: 9388
diff changeset
21 ## @deftypefn {Function File} {} repmat (@var{A}, @var{m}, @var{n})
3914
9eb96199e0f1 [project @ 2002-04-30 02:40:04 by jwe]
jwe
parents:
diff changeset
22 ## @deftypefnx {Function File} {} repmat (@var{A}, [@var{m} @var{n}])
9041
853f96e8008f Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents: 8970
diff changeset
23 ## @deftypefnx {Function File} {} repmat (@var{A}, [@var{m} @var{n} @var{p} @dots{}])
3914
9eb96199e0f1 [project @ 2002-04-30 02:40:04 by jwe]
jwe
parents:
diff changeset
24 ## Form a block matrix of size @var{m} by @var{n}, with a copy of matrix
9eb96199e0f1 [project @ 2002-04-30 02:40:04 by jwe]
jwe
parents:
diff changeset
25 ## @var{A} as each element. If @var{n} is not specified, form an
9eb96199e0f1 [project @ 2002-04-30 02:40:04 by jwe]
jwe
parents:
diff changeset
26 ## @var{m} by @var{m} block matrix.
10801
a40e32927b3a Improve documentation for new repelems function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
27 ## @seealso{repelems}
3914
9eb96199e0f1 [project @ 2002-04-30 02:40:04 by jwe]
jwe
parents:
diff changeset
28 ## @end deftypefn
9eb96199e0f1 [project @ 2002-04-30 02:40:04 by jwe]
jwe
parents:
diff changeset
29
9eb96199e0f1 [project @ 2002-04-30 02:40:04 by jwe]
jwe
parents:
diff changeset
30 ## Author: Paul Kienzle <pkienzle@kienzle.powernet.co.uk>
9eb96199e0f1 [project @ 2002-04-30 02:40:04 by jwe]
jwe
parents:
diff changeset
31 ## Created: July 2000
9eb96199e0f1 [project @ 2002-04-30 02:40:04 by jwe]
jwe
parents:
diff changeset
32
3915
2f341412622f [project @ 2002-04-30 02:41:04 by jwe]
jwe
parents: 3914
diff changeset
33 function x = repmat (a, m, n)
2f341412622f [project @ 2002-04-30 02:41:04 by jwe]
jwe
parents: 3914
diff changeset
34
3914
9eb96199e0f1 [project @ 2002-04-30 02:40:04 by jwe]
jwe
parents:
diff changeset
35 if (nargin < 2 || nargin > 3)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5443
diff changeset
36 print_usage ();
3914
9eb96199e0f1 [project @ 2002-04-30 02:40:04 by jwe]
jwe
parents:
diff changeset
37 endif
9eb96199e0f1 [project @ 2002-04-30 02:40:04 by jwe]
jwe
parents:
diff changeset
38
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4030
diff changeset
39 if (nargin == 3)
5195
5208182e2e62 [project @ 2005-03-08 19:04:12 by jwe]
jwe
parents: 4964
diff changeset
40 if (! (isscalar (m) && isscalar (n)))
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4030
diff changeset
41 error ("repmat: with 3 arguments m and n must be scalar");
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4030
diff changeset
42 endif
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4030
diff changeset
43 idx = [m, n];
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4030
diff changeset
44 else
4030
22bd65326ec1 [project @ 2002-08-09 18:58:13 by jwe]
jwe
parents: 3915
diff changeset
45 if (isscalar (m))
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4030
diff changeset
46 idx = [m, m];
3914
9eb96199e0f1 [project @ 2002-04-30 02:40:04 by jwe]
jwe
parents:
diff changeset
47 n = m;
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4030
diff changeset
48 elseif (isvector (m) && length (m) > 1)
8390
49901b624316 optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
49 ## Ensure that we have a row vector
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4030
diff changeset
50 idx = m(:).';
3914
9eb96199e0f1 [project @ 2002-04-30 02:40:04 by jwe]
jwe
parents:
diff changeset
51 else
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4030
diff changeset
52 error ("repmat: invalid dimensional argument");
3914
9eb96199e0f1 [project @ 2002-04-30 02:40:04 by jwe]
jwe
parents:
diff changeset
53 endif
9eb96199e0f1 [project @ 2002-04-30 02:40:04 by jwe]
jwe
parents:
diff changeset
54 endif
8508
dee629f14bfa repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents: 8507
diff changeset
55
dee629f14bfa repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents: 8507
diff changeset
56 if (all (idx < 0))
dee629f14bfa repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents: 8507
diff changeset
57 error ("repmat: invalid dimensions");
dee629f14bfa repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents: 8507
diff changeset
58 else
dee629f14bfa repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents: 8507
diff changeset
59 idx = max (idx, 0);
dee629f14bfa repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents: 8507
diff changeset
60 endif
3914
9eb96199e0f1 [project @ 2002-04-30 02:40:04 by jwe]
jwe
parents:
diff changeset
61
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4030
diff changeset
62 if (numel (a) == 1)
8390
49901b624316 optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
63 ## optimize the scalar fill case.
49901b624316 optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
64 x(1:prod (idx)) = a;
49901b624316 optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
65 x = reshape (x, idx);
49901b624316 optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
66 elseif (ndims (a) == 2 && length (idx) < 3)
49901b624316 optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
67 if (issparse (a))
9388
5556563c6551 repmat.m: call cron, not spkron
Marco Caliari <marco.caliari@univr.it>
parents: 9041
diff changeset
68 x = kron (ones (idx), a);
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4030
diff changeset
69 else
8390
49901b624316 optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
70 ## 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
71 m = rows (a); n = columns (a);
49901b624316 optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
72 p = idx(1); q = idx(2);
49901b624316 optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
73 x = reshape (a, m, 1, n, 1);
49901b624316 optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
74 x = x(:, ones (1, p), :, ones (1, q));
49901b624316 optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
75 x = reshape (x, m*p, n*q);
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4030
diff changeset
76 endif
3914
9eb96199e0f1 [project @ 2002-04-30 02:40:04 by jwe]
jwe
parents:
diff changeset
77 else
8970
b37a6c27c23f simplify repmat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
78 aidx = size (a);
b37a6c27c23f simplify repmat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
79 ## ensure matching size
b37a6c27c23f simplify repmat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
80 idx(end+1:length (aidx)) = 1;
b37a6c27c23f simplify repmat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
81 aidx(end+1:length (idx)) = 1;
b37a6c27c23f simplify repmat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
82 ## create subscript array
b37a6c27c23f simplify repmat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
83 cidx = cell (2, length (aidx));
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8390
diff changeset
84 for i = 1:length (aidx)
8970
b37a6c27c23f simplify repmat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
85 cidx{1,i} = ':';
b37a6c27c23f simplify repmat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
86 cidx{2,i} = ones (1, idx (i));
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4030
diff changeset
87 endfor
8970
b37a6c27c23f simplify repmat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
88 aaidx = aidx;
b37a6c27c23f simplify repmat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
89 # add singleton dims
b37a6c27c23f simplify repmat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
90 aaidx(2,:) = 1;
b37a6c27c23f simplify repmat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
91 a = reshape (a, aaidx(:));
b37a6c27c23f simplify repmat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
92 x = reshape (a (cidx{:}), idx .* aidx);
3914
9eb96199e0f1 [project @ 2002-04-30 02:40:04 by jwe]
jwe
parents:
diff changeset
93 endif
9eb96199e0f1 [project @ 2002-04-30 02:40:04 by jwe]
jwe
parents:
diff changeset
94
9eb96199e0f1 [project @ 2002-04-30 02:40:04 by jwe]
jwe
parents:
diff changeset
95 endfunction
6987
deb175b6e4a1 [project @ 2007-10-09 18:39:15 by jwe]
jwe
parents: 6046
diff changeset
96
deb175b6e4a1 [project @ 2007-10-09 18:39:15 by jwe]
jwe
parents: 6046
diff changeset
97 # Test various methods of providing size parameters
deb175b6e4a1 [project @ 2007-10-09 18:39:15 by jwe]
jwe
parents: 6046
diff changeset
98 %!shared x
deb175b6e4a1 [project @ 2007-10-09 18:39:15 by jwe]
jwe
parents: 6046
diff changeset
99 %! x = [1 2;3 4];
deb175b6e4a1 [project @ 2007-10-09 18:39:15 by jwe]
jwe
parents: 6046
diff changeset
100 %!assert(repmat(x, [1 1]), repmat(x, 1));
deb175b6e4a1 [project @ 2007-10-09 18:39:15 by jwe]
jwe
parents: 6046
diff changeset
101 %!assert(repmat(x, [3 3]), repmat(x, 3));
deb175b6e4a1 [project @ 2007-10-09 18:39:15 by jwe]
jwe
parents: 6046
diff changeset
102 %!assert(repmat(x, [1 1]), repmat(x, 1, 1));
deb175b6e4a1 [project @ 2007-10-09 18:39:15 by jwe]
jwe
parents: 6046
diff changeset
103 %!assert(repmat(x, [1 3]), repmat(x, 1, 3));
deb175b6e4a1 [project @ 2007-10-09 18:39:15 by jwe]
jwe
parents: 6046
diff changeset
104 %!assert(repmat(x, [3 1]), repmat(x, 3, 1));
deb175b6e4a1 [project @ 2007-10-09 18:39:15 by jwe]
jwe
parents: 6046
diff changeset
105 %!assert(repmat(x, [3 3]), repmat(x, 3, 3));
deb175b6e4a1 [project @ 2007-10-09 18:39:15 by jwe]
jwe
parents: 6046
diff changeset
106
deb175b6e4a1 [project @ 2007-10-09 18:39:15 by jwe]
jwe
parents: 6046
diff changeset
107 # Tests for numel==1 case:
deb175b6e4a1 [project @ 2007-10-09 18:39:15 by jwe]
jwe
parents: 6046
diff changeset
108 %!shared x, r
deb175b6e4a1 [project @ 2007-10-09 18:39:15 by jwe]
jwe
parents: 6046
diff changeset
109 %! x = [ 65 ];
deb175b6e4a1 [project @ 2007-10-09 18:39:15 by jwe]
jwe
parents: 6046
diff changeset
110 %! r = kron(ones(2,2), x);
deb175b6e4a1 [project @ 2007-10-09 18:39:15 by jwe]
jwe
parents: 6046
diff changeset
111 %!assert(r, repmat(x, [2 2]));
deb175b6e4a1 [project @ 2007-10-09 18:39:15 by jwe]
jwe
parents: 6046
diff changeset
112 %!assert(char(r), repmat(char(x), [2 2]));
deb175b6e4a1 [project @ 2007-10-09 18:39:15 by jwe]
jwe
parents: 6046
diff changeset
113 %!assert(int8(r), repmat(int8(x), [2 2]));
deb175b6e4a1 [project @ 2007-10-09 18:39:15 by jwe]
jwe
parents: 6046
diff changeset
114
deb175b6e4a1 [project @ 2007-10-09 18:39:15 by jwe]
jwe
parents: 6046
diff changeset
115 # Tests for ndims==2 case:
deb175b6e4a1 [project @ 2007-10-09 18:39:15 by jwe]
jwe
parents: 6046
diff changeset
116 %!shared x, r
deb175b6e4a1 [project @ 2007-10-09 18:39:15 by jwe]
jwe
parents: 6046
diff changeset
117 %! x = [ 65 66 67 ];
deb175b6e4a1 [project @ 2007-10-09 18:39:15 by jwe]
jwe
parents: 6046
diff changeset
118 %! r = kron(ones(2,2), x);
deb175b6e4a1 [project @ 2007-10-09 18:39:15 by jwe]
jwe
parents: 6046
diff changeset
119 %!assert(r, repmat(x, [2 2]));
deb175b6e4a1 [project @ 2007-10-09 18:39:15 by jwe]
jwe
parents: 6046
diff changeset
120 %!assert(char(r), repmat(char(x), [2 2]));
deb175b6e4a1 [project @ 2007-10-09 18:39:15 by jwe]
jwe
parents: 6046
diff changeset
121 %!assert(int8(r), repmat(int8(x), [2 2]));
deb175b6e4a1 [project @ 2007-10-09 18:39:15 by jwe]
jwe
parents: 6046
diff changeset
122
deb175b6e4a1 [project @ 2007-10-09 18:39:15 by jwe]
jwe
parents: 6046
diff changeset
123 # Tests for dim>2 case:
deb175b6e4a1 [project @ 2007-10-09 18:39:15 by jwe]
jwe
parents: 6046
diff changeset
124 %!shared x, r
deb175b6e4a1 [project @ 2007-10-09 18:39:15 by jwe]
jwe
parents: 6046
diff changeset
125 %! x = [ 65 66 67 ];
deb175b6e4a1 [project @ 2007-10-09 18:39:15 by jwe]
jwe
parents: 6046
diff changeset
126 %! r = kron(ones(2,2), x);
deb175b6e4a1 [project @ 2007-10-09 18:39:15 by jwe]
jwe
parents: 6046
diff changeset
127 %! r(:,:,2) = r(:,:,1);
deb175b6e4a1 [project @ 2007-10-09 18:39:15 by jwe]
jwe
parents: 6046
diff changeset
128 %!assert(r, repmat(x, [2 2 2]));
deb175b6e4a1 [project @ 2007-10-09 18:39:15 by jwe]
jwe
parents: 6046
diff changeset
129 %!assert(char(r), repmat(char(x), [2 2 2]));
deb175b6e4a1 [project @ 2007-10-09 18:39:15 by jwe]
jwe
parents: 6046
diff changeset
130 %!assert(int8(r), repmat(int8(x), [2 2 2]));
deb175b6e4a1 [project @ 2007-10-09 18:39:15 by jwe]
jwe
parents: 6046
diff changeset
131
deb175b6e4a1 [project @ 2007-10-09 18:39:15 by jwe]
jwe
parents: 6046
diff changeset
132 # Test that sparsity is kept
deb175b6e4a1 [project @ 2007-10-09 18:39:15 by jwe]
jwe
parents: 6046
diff changeset
133 %!assert(sparse(4,4), repmat(sparse(2,2),[2 2]));
deb175b6e4a1 [project @ 2007-10-09 18:39:15 by jwe]
jwe
parents: 6046
diff changeset
134
8508
dee629f14bfa repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents: 8507
diff changeset
135
dee629f14bfa repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents: 8507
diff changeset
136 %!assert (size (repmat (".", -1, 1)), [0, 1]);
dee629f14bfa repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents: 8507
diff changeset
137 %!assert (size (repmat (".", 1, -1)), [1, 0]);
dee629f14bfa repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents: 8507
diff changeset
138 %!error (size (repmat (".", -1, -1)));