last iteration of the for loop takes too long

1 vue (au cours des 30 derniers jours)
A
A le 26 Nov 2013
Réponse apportée : Sarthak le 20 Fév 2023
The last iteration of the outer loop (j=20) takes a very long time to exit- much longer than 1-19. When I debug line by line, it is at the last "end" where it gets stuck. Any ideas why?
for j=1:20
jj=size(start{j});
for i=1:jj(1)
[Ax Ay Az Bx By Bz]=calculate_segment_fields_BS_direct_matrix_notation(start{j}(i,:),endd{j}(i,:),phantom);
Axx(:,:,:,j)=Axx(:,:,:,j)+Ax;
Ayy(:,:,:,j)=Ayy(:,:,:,j)+Ay;
Azz(:,:,:,j)=Azz(:,:,:,j)+Az;
Bxx(:,:,:,j)=Bxx(:,:,:,j)+Bx;
Byy(:,:,:,j)=Byy(:,:,:,j)+By;
Bzz(:,:,:,j)=Bzz(:,:,:,j)+Bz;
waitbar(((j-1).*jj(1)+i)./jj(1)./20)
end
end

Réponses (1)

Sarthak
Sarthak le 20 Fév 2023
Hi,
It’s difficult to exactly know why this is happening, but what I have understood is that maybe the arrays Axx, Ayy, Azz, Bxx, Byy, and Bzz are growing in size and consuming too much memory, causing the code to run slowly. It is also possible that the waitbar function might be slowing down the code. You can try to remove it and see if that speeds up the code.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by