last iteration of the for loop takes too long

조회 수: 1 (최근 30일)
A
A 2013년 11월 26일
답변: Sarthak 2023년 2월 20일
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