Info

This question is closed. Reopen it to edit or answer.

Simplification of two for loops

1 view (last 30 days)
Ivan
Ivan on 12 Nov 2020
Closed: MATLAB Answer Bot on 20 Aug 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,

Answers (1)

Bruno Luong
Bruno Luong on 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;

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!