View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from

5.0 | 3 ratings Rate this file 10 Downloads (last 30 days) File Size: 1.88 KB File ID: #24536 Version: 1.1
image thumbnail



Loginatorist (view profile)


23 Jun 2009 (Updated )

Replicate and tile each element of an array.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information

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:


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.


>> 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.


Replicate inspired this file.

MATLAB release MATLAB 7.4 (R2007a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (6)
05 Mar 2015 Nike Dattani

I've been using EXPAND for a number of years, and it's part of my open source software FeynDyn for numerically calculating Feynman integrals.

Regarding Yair Altman's comment below:
Have the speed and memory of EXPAND been retested now?

I'm wondering if I should scrap EXPAND from my software.

Also, MATLAB has it's own EXPAND in the Symbolic Toolbox for expanding expressions like (x+y)^2.

Thanks for the input.

13 Jul 2013 Yair Altman

Yair Altman (view profile)

Now that R2013b has finally incorporated Bruno's efficient implementation (FEX 24499) into the built-in KRON function, I expect the balance of performance and memory to shift in favor of KRON. Of course, expand will continue to be better for anyone still using R2013a or earlier.

For additional alternative implementations, see

24 Jun 2009 Loginatorist

Loginatorist (view profile)

Another reason I bypassed kron is illustrated here:

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

Comment only
24 Jun 2009 us

us (view profile)

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:

24 Jun 2009 Loginatorist

Loginatorist (view profile)

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];
T = expand(A,SZ);
toc %Elapsed time is 0.043622 seconds.
T2 = kron(A,ones(SZ));
toc %Elapsed time is 0.082535 seconds.

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.

Comment only
24 Jun 2009 Jos (10584)

Jos (10584) (view profile)

Why not simply

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!

Comment only
24 Jun 2009 1.1

Added kron to the see also list.

Contact us