How to avoid double for-loop

7 ビュー (過去 30 日間)
Lucius
Lucius 2015 年 4 月 21 日
コメント済み: Star Strider 2015 年 4 月 21 日
I have the following code
for n = 1:N
for l = 1:N
phase = exp(1i * (rand*0.5*pi-0.25*pi));
Efield_x(n,l) = Efield_x(n,l) .* phase;
end
end
with N=2^10 . Each matrix element should be multiplied with a different random number from -0.25*pi to 0.25*pi . Although the computer is running ("busy") I am waiting for 15 minutes now. How can we avoid this slow double for-loop to fasten to process significantly? By the way, I have 8 GB Ram.

採用された回答

Star Strider
Star Strider 2015 年 4 月 21 日
This seems to work:
N = 2^10;
Efield_x = ones(N); % Create Data
phase = exp(1i * (rand(N)-0.5)*0.25*pi);
Efield_x = Efield_x .* phase;
  2 件のコメント
Lucius
Lucius 2015 年 4 月 21 日
編集済み: Lucius 2015 年 4 月 21 日
you mean
...(rand(N)-0.5)*0.5*pi);
This works, thanks.
Star Strider
Star Strider 2015 年 4 月 21 日
My pleasure.
You’re correct on ‘phase’, with 0.5 rather than 0.25 as the multiplier to produce ±0.25*pi.

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

その他の回答 (0 件)

カテゴリ

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