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!

 Akzeptierte Antwort

Matt Fig
Matt Fig am 4 Sep. 2012
Bearbeitet: Matt Fig am 4 Sep. 2012

2 Stimmen

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 Kommentare

Azzi Abdelmalek
Azzi Abdelmalek am 4 Sep. 2012
how much longer?
Oleg Komarov
Oleg Komarov am 4 Sep. 2012
Elapsed time is 45.431542 seconds.
Elapsed time is 1.081703 seconds.
Azzi Abdelmalek
Azzi Abdelmalek am 4 Sep. 2012
Thanks Oleg
Matt Fig
Matt Fig am 4 Sep. 2012
Bearbeitet: Matt Fig am 5 Sep. 2012
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 am 4 Sep. 2012
Thanks Matt, it seems Oleg's machine is MUCH faster then yours
Matt Fig
Matt Fig am 4 Sep. 2012
I agree, that's why I asked what he is using ;-).
Alireza
Alireza am 4 Sep. 2012
Bearbeitet: Walter Roberson am 4 Sep. 2012
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 am 4 Sep. 2012
Bearbeitet: Matt Fig am 4 Sep. 2012
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 am 4 Sep. 2012
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 am 4 Sep. 2012
@Matt: R2012a win7 64 on i7-2600 3.40GHz

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Noch keine Tags eingegeben.

Community Treasure Hunt

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

Start Hunting!

Translated by