last iteration of the for loop takes too long

1 view (last 30 days)
A
A on 26 Nov 2013
Answered: Sarthak on 20 Feb 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

Answers (1)

Sarthak
Sarthak on 20 Feb 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.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!