for loop over subset - finding indices vs if-clause
Mostra commenti meno recenti
I want to compare two-cell arrays (X and Y) field by field (each field is a 2-dimensional array of points) and for each comparison compute how many points overlap. However I have a condition that needs to be fulfilled in each instance (which depends on some numbers in an array). Which of the following approaches makes more sense (speed or other issues)? Is there even a significant difference?
edit: The condition to check consists of two number comparisons, i.e. if/find array(jj)>= 0.5*K && array(jj)<=2*K, where K is constant for one ii.
1. find indices beforehand and run the second for-loop only over those
for ii=1:N
idx = find(jj that fulfill CONDITION in array);
for jj=idx
matrix(ii,jj) = sum(ismember(X{ii}, Y{jj}));
end
end
2. run for-loop over all indices and check condition individually with an if-clause
for ii=1:N
for jj=1:M
if Y(jj) fulfills CONDITION in array
matrix(ii,jj) = sum(ismember(X{ii}, Y{jj}));
end
end
end
Risposta accettata
Più risposte (1)
Jan
il 5 Lug 2021
1 voto
It depends. How expensive is "fulfill CONDITION"?
Simply try it. Implement both versions and measure the timings with tic/toc.
1 Commento
Felix Müller
il 5 Lug 2021
Categorie
Scopri di più su Profile and Improve Performance in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!