vectorize a block matrix calculation

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;

 採用された回答

Matt J
Matt J 2017 年 4 月 28 日

0 投票

TP=kron(a(:),xx);

3 件のコメント

Luis Isaac
Luis Isaac 2017 年 4 月 28 日
Many thaks Matt
I wondered, if I would want to calculate the sum, what is the correct operation. I mean:
...
TP((i-1)*n+1:i*n,:)=a(i)+xx; % Block i of TP matrix
...
Thanks in advanced;
Andrei Bobrov
Andrei Bobrov 2017 年 4 月 28 日
TP = reshape(bsxfun(@plus,xx',reshape(a,1,1,[])),size(x,2),[])'
Matt J
Matt J 2017 年 4 月 28 日
編集済み: Matt J 2017 年 4 月 28 日
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.

サインインしてコメントする。

その他の回答 (0 件)

タグ

質問済み:

2017 年 4 月 28 日

編集済み:

2017 年 4 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by