Replace loop with more efficient procedure to make it faster
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
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?
Accepted Answer
Fangjun Jiang
on 19 Aug 2021
Would this give you a clue?
t1=1:3;
t2=(1:4)';
flag=(t1==t2)
9 Comments
Lukas Netzer
on 19 Aug 2021
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
on 19 Aug 2021
Edited: Fangjun Jiang
on 19 Aug 2021
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.
Lukas Netzer
on 19 Aug 2021
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!
dpb
on 19 Aug 2021
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.
Lukas Netzer
on 19 Aug 2021
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 :)
dpb
on 19 Aug 2021
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
on 20 Aug 2021
Edited: Lukas Netzer
on 20 Aug 2021
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.
Fangjun Jiang
on 20 Aug 2021
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.
Peter Perkins
on 2 Sep 2021
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.
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Tags
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)