vectorize a block matrix calculation

2 views (last 30 days)
Luis Isaac
Luis Isaac on 28 Apr 2017
Edited: Matt J on 28 Apr 2017
Dear;
I want calculate a big matrix composed of blocks, for that I use the following code:
k=length(a); % Long vector
n=size(xx,1); % 2D or 3D matrix always the same
TP=repmat(zeros(size(xx),k,1); % Big matriz composed by blocks
for i=1:k
TP((i-1)*n+1:i*n,:)=a(i)*xx; % Block i of TP matrix
end
Is there any way to vectorize this code and speed up the calculation Many thanks;

Accepted Answer

Matt J
Matt J on 28 Apr 2017
TP=kron(a(:),xx);
  3 Comments
Andrei Bobrov
Andrei Bobrov on 28 Apr 2017
TP = reshape(bsxfun(@plus,xx',reshape(a,1,1,[])),size(x,2),[])'
Matt J
Matt J on 28 Apr 2017
Edited: Matt J on 28 Apr 2017
You could also use my attached tensorfun() utility.
TP=tensorfun(@plus,a(:),xx);
It allows you to do similar things for any block structure and any bi-operand operation (@plus, @minus, etc...) supported by bsxfun.

Sign in to comment.

More Answers (0)

Categories

Find more on System Composer in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!