Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

Simplification of two for loops

3 vues (au cours des 30 derniers jours)
Ivan
Ivan le 12 Nov 2020
Clôturé : MATLAB Answer Bot le 20 Août 2021
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,

Réponses (1)

Bruno Luong
Bruno Luong le 12 Nov 2020
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;

Cette question est clôturée.

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by