MATLAB Answers

Is there a way to vectorise this vector/matrix product?

3 ビュー (過去 30 日間)
Daniel Pollard
Daniel Pollard 2021 年 5 月 7 日
回答済み: Bruno Luong 2021 年 5 月 7 日
I have a piece of code inside a for loop, which looks like
for k = 1:size(G, 1)
G(k) = real(W(:,k)' * R * W(:,k))
end
G is preallocated (which is how I know the length of it). W and R are 2D matrices (so W(:,k) is a vector). This loop is a real bottleneck in my code, that one line taking 90% of the total time that it takes to run a script which itself calls other functions and files. It's not possible to use a parfor loop as I'm already using one in a wrapper function.
Is there a way to vectorise that loop, in order to reduce the number of times the calculation is done? I'm sure it can be done, but it's not at all obvious to me.

採用された回答

Matt J
Matt J 2021 年 5 月 7 日
編集済み: Matt J 2021 年 5 月 7 日
G=real( sum((R*W).*conj(W),1) );
  4 件のコメント
Daniel Pollard
Daniel Pollard 2021 年 5 月 7 日
You're right, and I just came back to your answer to say that. Thank you!

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

その他の回答 (1 件)

Bruno Luong
Bruno Luong 2021 年 5 月 7 日
G = real(dot(W,R*W,1)).';

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by