Efficient moving average of scattered data
28 views (last 30 days)
I have some scattered data and I'd like to take something similar to a moving average, where I average all values with in some radius of each point. I can do this with a loop, but I'd like a more efficient approach. Any ideas?
Here's a working example I'd like to make more efficient:
x = randi(100,45,1) + 20+3*randn(45,1) ;
y = 15*sind(x) + randn(size(x)) + 3;
radius = 10;
ymean = NaN(size(x));
for k = 1:length(x)
% Indicies of all points within specified radius:
ind = abs(x-x(k))<radius;
% Mean of y values within radius:
ymean(k) = mean(y(ind));
legend('scattered data','radial average','location','southeast')
More Answers (2)
Chris Turnes on 9 Mar 2017
If you can upgrade to R2017a, this functionality can now be achieved through the 'SamplePoints' name-value pair in the moving statistics. For your example, you would do something like movmean(y, 2*radius, 'SamplePoints', x); (though you'd need to sort your x values first).
Walter Roberson on 28 Jun 2016
pdist() to get all of the distances simultaneously. Compare to the radius. Store the resulting mask. Multiply the mask by repmat() of the y value, and sum along a dimension. sum the mask along the same dimension and divide the value sum by that count. Result should be the moving average.