現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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!
採用された回答
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
2012 年 9 月 4 日
how much longer?
Oleg Komarov
2012 年 9 月 4 日
Elapsed time is 45.431542 seconds.
Elapsed time is 1.081703 seconds.
Azzi Abdelmalek
2012 年 9 月 4 日
Thanks Oleg
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
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
2012 年 9 月 4 日
Thanks Matt, it seems Oleg's machine is MUCH faster then yours
Matt Fig
2012 年 9 月 4 日
I agree, that's why I asked what he is using ;-).
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!
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
2012 年 9 月 4 日
Reformatted for you. Please read http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup
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
2012 年 9 月 4 日
@Matt: R2012a win7 64 on i7-2600 3.40GHz
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Logical についてさらに検索
タグ
タグが未入力です。
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
