Avoid for loops with if inside

Hello everyone,
I have a code as follows:
for i=1:10000
for j=1:5000
if (A(1,i)-B(1,j)<5)
C(i,j)=C(i,j)+1;
end
end
end
Can anyone show me how I can avoid these for loops, so that my run can be much faster? Any help is highly appreciated. Thanks!

 채택된 답변

Matt Fig
Matt Fig 2012년 9월 4일
편집: Matt Fig 2012년 9월 4일

2 개 추천

idx = bsxfun(@minus,A(1,:).',B(1,:))<5;
C(idx) = C(idx) + 1;
It would help if you specified the sizes of A, B and C. I assume A and B are matrices, but are they vectors or what?
Here is how I tested the above code:
A = round(rand(1,10000)*10);
B = round(rand(1,5000)*10);
C = round(rand(10000,5000)*10);
C2 = C;
tic
for i=1:size(A,2)
for j=1:size(B,2)
if (A(1,i)-B(1,j)<5)
C(i,j)=C(i,j)+1;
end
end
end
toc
tic
idx = bsxfun(@minus,A(1,:).',B(1,:))<5;
C2(idx) = C2(idx) + 1;
toc
isequal(C,C2) % Yes... And the loops take MUCH longer!

댓글 수: 12

Azzi Abdelmalek
Azzi Abdelmalek 2012년 9월 4일
how much longer?
Oleg Komarov
Oleg Komarov 2012년 9월 4일
Elapsed time is 45.431542 seconds.
Elapsed time is 1.081703 seconds.
Azzi Abdelmalek
Azzi Abdelmalek 2012년 9월 4일
Thanks Oleg
Matt Fig
Matt Fig 2012년 9월 4일
편집: Matt Fig 2012년 9월 5일
Wow, Oleg! What machine are you using, may I ask? On my machine the times are:
Elapsed time is 201.455394 seconds.
Elapsed time is 4.744238 seconds.
Incidentally, this is nearly as fast as full vectorization:
for ii=1:size(A,2)
idx = A(1,ii)-B(1,:)<5;
C3(ii,idx) = C3(ii,idx)+1;
end
Azzi Abdelmalek
Azzi Abdelmalek 2012년 9월 4일
Thanks Matt, it seems Oleg's machine is MUCH faster then yours
Matt Fig
Matt Fig 2012년 9월 4일
I agree, that's why I asked what he is using ;-).
Alireza
Alireza 2012년 9월 4일
편집: Walter Roberson 2012년 9월 4일
Thank you very much for your reply!
Well, my actual code is as follows, which is quite different from my original question:
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!
Matt Fig
Matt Fig 2012년 9월 4일
편집: Matt Fig 2012년 9월 4일
Now why would you ask a question, have people spend time on it, then tell them that it isn't the real question? Please close this question then ask your new question - and give the real question the first time...
Alireza
Alireza 2012년 9월 4일
Sorry! But I really didn't mean to bother anyone. That was a misunderstanding since I was not very familiar with guidelines. I will ask my question as a new one. Thanks!
Oleg Komarov
Oleg Komarov 2012년 9월 4일
@Matt: R2012a win7 64 on i7-2600 3.40GHz

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

태그

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

질문:

2012년 9월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by