Replace loop with more efficient procedure to make it faster

5 vues (au cours des 30 derniers jours)
Lukas Netzer
Lukas Netzer le 19 Août 2021
Commenté : Peter Perkins le 2 Sep 2021
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?

Réponse acceptée

Fangjun Jiang
Fangjun Jiang le 19 Août 2021
Would this give you a clue?
t1=1:3;
t2=(1:4)';
flag=(t1==t2)
  9 commentaires
Fangjun Jiang
Fangjun Jiang le 20 Août 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
Peter Perkins le 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by