How can I vectorize this for-loop

Hi,
I have the following loop that is called lots of times within a function and is slowing down considerably
n=200;
F1=rand(n,4);
F2=rand(n,4);
dRad_dt=zeros(n,4)
for i=1:4
for j=1:n
for k=1:n
if j+k<=n
dRad_dt(j+k,i)=dRad_dt(j+k,i)+F1(j,i)*F2(k,i);
end
end
end
end
Any clues on how to vectorize this code or speed up considerably would be greatly appreciated

 採用された回答

Matt J
Matt J 2016 年 1 月 31 日
編集済み: Matt J 2016 年 1 月 31 日

1 投票

You've re-invented convolution,
dRad_dt=zeros(n,4);
tmp = fft(F1,2*n,1).*fft(F2,2*n,1); %fft-based convolution
tmp= ifft(tmp, 'symmetric');
dRad_dt(2:n,:) = tmp(1:n-1,:);

1 件のコメント

Nicholas
Nicholas 2016 年 2 月 1 日
Thanks, Works a charm.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeProbability Distributions and Hypothesis Tests についてさらに検索

質問済み:

2016 年 1 月 31 日

コメント済み:

2016 年 2 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by