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
0 个评论
回答(1 个)
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!