Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Matrix replication

Subject: Matrix replication

From: George Papazafeiropoulos

Date: 31 Oct, 2013 21:35:36

Message: 1 of 3

Hi all,

Here is a simple code in which a given matrix is replicated in five different ways:

% Matrix to be replicated
b=100*rand(12,12,2000);
% 1
tic
Id1 = (1 : size(b,1))';
Id2 = (1 : size(b,2))';
Id3 = (1 : size(b,3))';
Id4 = (1 : size(b,4))';
x1=b(Id1(:,ones(1,1)), Id2(:,ones(1,1)), Id3(:,ones(1,1)),...
    Id4(:,ones(50),1));
toc
clear x1
% 2
tic
x2 = bsxfun(@plus, zeros(1,1,1,50), b);
toc
clear x2
% 3
tic
x3 = bsxfun(@times, ones(1,1,1,50), b);
toc
clear x3
% 4
tic
x4=repmat(b,[1 1 1 50]);
toc
clear x4
% 5
tic
x5=reparray(b,[1 1 1 50]);
toc
clear x5

Is anybody aware of another way to replicate matrices faster than the above? Or is there any possibility to improve appropriately any of the above methods to run faster?
Thank you in advance.

George Papazafeiropoulos

Subject: Matrix replication

From: James Tursa

Date: 1 Nov, 2013 15:28:08

Message: 2 of 3

"George Papazafeiropoulos" <gpapazafeiropoulos@yahoo.gr> wrote in message <l4uif8$kes$1@newscl01ah.mathworks.com>...
> Hi all,
>
> Here is a simple code in which a given matrix is replicated in five different ways:
>
> % Matrix to be replicated
> b=100*rand(12,12,2000);
> % 1
> tic
> Id1 = (1 : size(b,1))';
> Id2 = (1 : size(b,2))';
> Id3 = (1 : size(b,3))';
> Id4 = (1 : size(b,4))';
> x1=b(Id1(:,ones(1,1)), Id2(:,ones(1,1)), Id3(:,ones(1,1)),...
> Id4(:,ones(50),1));
> toc
> clear x1
> % 2
> tic
> x2 = bsxfun(@plus, zeros(1,1,1,50), b);
> toc
> clear x2
> % 3
> tic
> x3 = bsxfun(@times, ones(1,1,1,50), b);
> toc
> clear x3
> % 4
> tic
> x4=repmat(b,[1 1 1 50]);
> toc
> clear x4
> % 5
> tic
> x5=reparray(b,[1 1 1 50]);
> toc
> clear x5
>
> Is anybody aware of another way to replicate matrices faster than the above? Or is there any possibility to improve appropriately any of the above methods to run faster?
> Thank you in advance.

You can also play outer product games. E.g.,

x6 = reshape(b(:)*ones(1,50),[size(b) 50]);

Or resort to mex routines. E.g., if you have an OPENMP compliant C compiler available you could do the outer product in a multi-threaded way (MTIMESX using OPENMP typically beats MATLAB for speed for outer product calculations):

x7 = reshape(mtimesx(b(:),ones(1,50),'SPEEDOMP'),[size(b) 50]);

You can find MTIMESX here:

http://www.mathworks.com/matlabcentral/fileexchange/25977-mtimesx-fast-matrix-multiply-with-multi-dimensional-support

When timing any of these methods, btw, don't use the first timing because that always includes the function loading and parsing time as well.

James Tursa

Subject: Matrix replication

From: Bruno Luong

Date: 1 Nov, 2013 18:56:06

Message: 3 of 3

Another way

x = b(:,:,:,ones(1,50));

Bruno

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us