Need help to vectorize this small code
정보
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
이전 댓글 표시
Hi,
i would need help to vectorize this short code:
for k=1:N
for w=1:N
u=(k-(N/2)); v=(w-(N/2));
cube=(u^2+v^2);
S(k,w)=exp(i*pi*l*cube);
end
end
l and pi are numbers. thanks a lot in advance!!
best
댓글 수: 0
답변 (2개)
[u, v] = ndgrid(1-N/2:N/2);
cube = u.^2 + v.^2;
S = exp(1i*pi*l*cube)
댓글 수: 1
Jean-Nicolas Longchamp
2015년 12월 8일
Jan
2015년 12월 8일
I assume your want to vectorize the code to increase the processing speed. In your example the exp() function is more expensive than the loops. You can reduce the number of exp calls:
u = (1-N/2):(N/2);
expU = exp(i * pi * l * u.^2);
S = bsxfun(@times, expU, expU.');
댓글 수: 1
Jean-Nicolas Longchamp
2015년 12월 8일
이 질문은 마감되었습니다.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!