Replace loop with more efficient procedure to make it faster
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
So I'm running this loop:
for n = 1:size(table1)
for m = 1:size(table2)
if table1.WP1(n) == table2.WP1(m) && table1.WP2(n) == table2.WP2(m)
table2.STOPS(n) = table2.STOPS(m);
end
end
end
Which takes really long to process - is there a faster way to do it?
채택된 답변
Fangjun Jiang
2021년 8월 19일
Would this give you a clue?
t1=1:3;
t2=(1:4)';
flag=(t1==t2)
댓글 수: 9
Hey Fangjun,
thanks for your answer. So I'm creating an Index where matches happen and apply it somehow to the stops? :p
Fangjun Jiang
2021년 8월 19일
편집: Fangjun Jiang
2021년 8월 19일
When applying to your case, you will have flag1 based on "WP1" value, flag2 based on "WP2" value. then
flag= flag1 & flag2
use find() to get the index n and m
[IndexM, IndexN]=find(flag)
Then do this
table1.STOPS(IndexN)=table2.STOPS(IndexM)
No loops are needed.
thanks again for your detailed answer - so I ran the following code:
flag1 = (table1.WP1 == table2.WP1');
flag2 = (table1.WP2 == table2.WP2');
flag = flag1 & flag2;
[IndexM, IndexN]=find(flag);
wp{n}.STOPS(IndexM)=wps{n}.STOPS(IndexN);
notice the Indexes had to be switched. It's a really nice way to use Index - thanks for showing me. It's kind of hard to get my brain into it though.. have a nice day!
Nota Bene:
The above is not the same logic as the original code, however. This is a point-by-point comparison of the two and also requires they be the same length, the other is a comparison of the first to all elements of the second.
You need to be sure you have the desired solution.
Thank you - as a matter of fact they don't have the same length. That's why I had two loops. I first encountered a problem when running the following code:
flag1 = (table1.WP1 == table2.WP1);
flag2 = (table1.WP2 == table2.WP2);
but when changing it to:
flag1 = (table1.WP1 == table2.WP1');
flag2 = (table1.WP2 == table2.WP2');
it actually worked, so it should be kind of the same, at least the result is?
If you have any advice on making it more like my slow attempt (but faster), Í'd gladly appreciate it :)
Ah...by transposing you invoked the silent array expansion option -- and so then the two logicals are not vectors but arrays and, then in fact, are the same result.
So then, the Q? is, do you want the many-to-one relationship or a 1:1?
What is the expected size of the result -- possibly variable or just one element from the second for each n record in the first?
table2.STOPS(n) = table2.STOPS(m);
in the above double loop results in there being one value in the LHS but it will be the last matching location where WP1 and WP2 match (if there can be more than one).
Alternatively, the cell array solution could keep however many matches there are for each n.
You've not precisely defined which solution is the wanted one -- the first code will run and give a specific solution; the alternative can also run but may provide a different solution if there can be more than a single match.
If the data are such that only one match is known to be possible or only the first is wanted, then the double loop could be made to run faster by exiting the inner loop one a match is found.
But, we don't know which is the actual solution wanted...
Lukas Netzer
2021년 8월 20일
편집: Lukas Netzer
2021년 8월 20일
yes, only one solution can fit - only one match is possible. thanks for your explanation :)
one more question if I may:
I tried making the following code faster:
for x = 1:size(wp{n})
for y = 1:size(Locations_all)
if ismember(wp{n}.Locidx(x),Locations_all(y)) == 1 && wp{n}.STOPS(x) == 1
wp{n}.s_dur(x) = AccTimesAvg.(tt{n})(y);
continue
end
end
end
by applying the same technique:
flag1 = ((ismember(wp{n}.Locidx,Locations_all)) == 1 );
flag2 = (wp{n}.STOPS == 1);
flag = flag1 & flag2;
[IndexM, IndexN]=find(flag);
wp{n}.s_dur(IndexM)=AccTimesAvg.(tt{n})(IndexN);
again only one can fit. AccTimesAvg has 82 different values. Unfortunately when running the above code only one value gets printed at the right locations - where am I missing out?
IndexN is all 1's, where it should be values between 1 and 82.
The code I provided is for multiple matching. All can be done in one shot without loops.
If you know there is only one match, the double-for loop could be faster. Your need to use "break" (instead of "continue") to break the loop.
But again, it "could" be faster. If the matching one is the last iteration in the double-for loop, then the "break" has no effect.
The double loop is unlikely to be faster for any case. If there is at most one match in table2 for each row of table1, then perhaps ismember would work. But unless these tables are pretty big, the "row == col" strategy is likely to be the fastest.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
참고 항목
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)
