efficient use of find and ismember

7 visualizzazioni (ultimi 30 giorni)
Dan
Dan il 24 Apr 2014
Risposto: dpb il 25 Apr 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

Risposta accettata

dpb
dpb il 25 Apr 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...

Più risposte (0)

Categorie

Scopri di più su Structures in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by