How to avoid double for-loop
조회 수: 9 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
채택된 답변
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
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 Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!