Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Simplification of two for loops

조회 수: 1 (최근 30일)
Ivan
Ivan 2020년 11월 12일
마감: MATLAB Answer Bot 2021년 8월 20일
Hi,
I would like to simplify the two for loop to accelerate my calculation time
Maybe, you have an idea how can I do it. My code draw inside the Matrix M(N,N) the circles with following function
M(i,j)=(1-sin((pi*rad)/(2*radius1)))*exp(w*rad).
So, I have:
M=zeros(N,N);
for i=1:N
for j=1:N
if (i-i1)^2+(j-j1)^2 < radius1^2
radius = sqrt((i-i1)^2+(j-j1)^2);
M(i,j)=(1-sin((pi*radius)/(2*radius1)))*exp(w*radius);
end
end
end
Thank you in advance for any better suggestions!!!
Best regards,

답변 (1개)

Bruno Luong
Bruno Luong 2020년 11월 12일
i = (1:N)';
j = 1:N;
radius = sqrt((i-i1).^2+(j-j1).^2);
M = (1-sin((pi*radius)/(2*radius1))).*exp(w*radius);
M(radius>=radius1) = 0;

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by