efficient use of find and ismember

7 views (last 30 days)
Dan
Dan on 24 Apr 2014
Answered: dpb on 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

Accepted Answer

dpb
dpb on 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...

More Answers (0)

Community Treasure Hunt

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

Start Hunting!