이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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.
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!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
