last iteration of the for loop takes too long

1 次查看(过去 30 天)
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

回答(1 个)

Sarthak
Sarthak 2023-2-20
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.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by