how can i vectorize this loop?

2 ビュー (過去 30 日間)
Miguel Reina
Miguel Reina 2018 年 1 月 21 日
回答済み: Matt J 2018 年 1 月 22 日
K=100;
a=zeros(K,1);
for u = 0:K-1
for b = 0:K-1
a(u+1) = a(u+1) + s(b+1)*exp((-2j*pi*u*b/K));
end
end
  2 件のコメント
Matt J
Matt J 2018 年 1 月 21 日
What is "k" in s(k+1)? DO you mean s(b+1)?
Miguel Reina
Miguel Reina 2018 年 1 月 22 日
Yes is b+1

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

採用された回答

Matt J
Matt J 2018 年 1 月 21 日

その他の回答 (2 件)

Image Analyst
Image Analyst 2018 年 1 月 21 日
Well you could try meshgrid() but I think it might make it a little more confusing to understand what it's doing. With only 100x100 array to fill, a double for loop will be very fast so even if you did vectorize it, don't expect to notice any speed difference.

Matt J
Matt J 2018 年 1 月 22 日
A less efficient way,
z=0:K;
a=exp((-2j*pi/K)*z.'*z)*s(:);

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by