Avoid for loops with if inside

조회 수: 11 (최근 30일)
Alireza
Alireza 2012년 9월 4일
Hello everyone,
This is my code:
counter=1;
for i=1:size(A,1)
for j=counter:size(B,1)
if D(1,B(j,1))>0
if B(j,2)-A(i,2)<5
C(A(i,1),B(j,1))=C(A(i,1),B(j,1))+1;
D(1,B(j,1))=D(1,B(j,1))-1;
B(j,3)=1;
counter=j+1;
end
break;
end
end
end
A is an 100*3 matrix B is an 1000*3 matrix C is an 100*1000 matrix D is an 1*1000 matrix
So, can it be written by much less computational effort? Any comment is very appreciated! Thanks!

답변 (1개)

Doug Hull
Doug Hull 2012년 9월 6일
If it is working, what are you trying to accomplish by changing it. Have you run it through the profiler to find out that this is the bottleneck in your code slowing it down?
  댓글 수: 1
Alireza
Alireza 2012년 9월 6일
Thank you! Yes, you are right! That was not the bottleneck of my code. I checked the run time and that was fine.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by