5.0

5.0 | 1 rating Rate this file 111 downloads (last 30 days) File Size: 1.88 KB File ID: #24536

EXPAND

by Matt Fig

 

23 Jun 2009 (Updated 24 Jun 2009)

Code covered by BSD License  

Replicate and tile each element of an array.

Download Now | Watch this File

File Information
Description

EXPAND(A,SZ), for array A and vector SZ replicates each element of A by SZ. The results are tiled into an array in the same order as the elements of A, so that the result is size: size(A).*SZ.

Therefore the number of elements of SZ must equal the number of dimensions of A, or in MATLAB syntax:

length(size(A))==length(SZ)

must be true.
The result will have the same number of dimensions as does A.
There is no restriction on the number of dimensions for input A.

Examples:

>> A = [1 2;3 4]
A =
     1 2
     3 4
>> expand(A,[2 1])
ans =
     1 2
     1 2
     3 4
     3 4
>> expand(A,[2 2])
ans =
     1 1 2 2
     1 1 2 2
     3 3 4 4
     3 3 4 4

Please email me if bugs are found in the code. Thanks.

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
REPLICATE

MATLAB release MATLAB 7.4 (R2007a)
Zip File Content  
Other Files expand.m,
license.txt
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (4)
24 Jun 2009 Jos (10584)

Why not simply
kron(A,eyes(SZ))

or, for non-numerical arrays,
X = {'a' 'b' ; [1:3] []}
X(kron(reshape(1:numel(X),size(X)), ones(2,3)))
%ans =
% 'a' 'a' 'a' 'b' 'b' 'b'
% 'a' 'a' 'a' 'b' 'b' 'b'
% [1x3 double] [1x3 double] [1x3 double] [] [] []
% [1x3 double] [1x3 double] [1x3 double] [] [] []

It can be expanded to the ND case using reshape etc ...

Good help though!

24 Jun 2009 Matt Fig

Hello Jos,

There are a couple of reasons I bypassed kron. The main reason is that expand, even with error checking etc., is faster on my machine(s):

A = reshape(randperm(24),6,4);
SZ = [300,400];
tic
T = expand(A,SZ);
toc %Elapsed time is 0.043622 seconds.
tic
T2 = kron(A,ones(SZ));
toc %Elapsed time is 0.082535 seconds.

For
A = reshape(randperm(144),18,8);
SZ = [300,400];
expand is .25 vs. .78 seconds for kron. 2007b win xp.

expand also works for your second example.
Thanks.

24 Jun 2009 us

just like jos, i also felt at first that this - admittedly sleek engine - was more or less a KRON clone; but then your timing convinced me that there was clearly more to it...
however, i think you should at least mention KRON in a
see also:
statement...
us

24 Jun 2009 Matt Fig

Another reason I bypassed kron is illustrated here:

http://i217.photobucket.com/albums/cc229/spamanon/tester_output-2.png

Urs, I think you are correct about mentioning kron in the see also.

Please login to add a comment or rating.
Updates
24 Jun 2009

Added kron to the see also list.

Tag Activity for this File
Tag Applied By Date/Time
replicate Matt Fig 24 Jun 2009 09:17:36
expansion Matt Fig 24 Jun 2009 09:17:37
expand Matt Fig 24 Jun 2009 09:17:37
repmat Matt Fig 24 Jun 2009 09:17:37
tile Matt Fig 24 Jun 2009 09:17:37
tiling Matt Fig 24 Jun 2009 09:17:37
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com