Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Topic: Matrix replication
Replies: 2   Last Post: Nov 1, 2013 2:56 PM

 Messages: [ Previous | Next ]
 George Posts: 14 Registered: 8/19/10
Matrix replication
Posted: Oct 31, 2013 5:35 PM

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?