efficient use of find and ismember

6 vues (au cours des 30 derniers jours)
Dan
Dan le 24 Avr 2014
Réponse apportée : dpb le 25 Avr 2014
Hello,
I have 2-D array (~250000 x 2) that has repeated rows. I need to find the indices of the repeated rows and take a mean over those rows with another variable. The code I have below works fine, but it is VERY slow. In this example the variable 'latlon' is the 2-D array (1st column is latitude and 2nd column is longitude).
uniq = unique([latlon],'rows');
for i = 1:length(uniq)
pairs = find(ismember(latlon,uniq(i,:),'rows'));
%use pairs indices to grab data from another 1-D variable
newdata(i) = mean(SLP(pairs))
end
This currently takes ~4hrs to run for the 250,000 row array. Can anyone help me improve the efficiency here?
Thanks,
Dan

Réponse acceptée

dpb
dpb le 25 Avr 2014
The "deadahead" solution (w/o accumarray, I'm to brain-tired this evening to write that off the cuff... :) )
[~,ia,ic]=unique(latlon,'rows','stable');
new=zeros(size(ia));
for i=1:length(ia)
new(i) = mean(SLP(ia(i)==ic));
end
I think I got that right...

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by