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.
Matt Fig
Matt Fig 2012 年 9 月 4 日
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...
Walter Roberson
Walter Roberson 2012 年 9 月 4 日
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 件)

カテゴリ

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by