Efficient moving average of scattered data

조회 수: 4 (최근 30일)
Chad Greene
Chad Greene 2016년 6월 28일
답변: Chris Turnes 2017년 3월 9일
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;
figure
plot(x,y,'bo')
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));
end
hold on
plot(x,ymean,'ks')
legend('scattered data','radial average','location','southeast')
  댓글 수: 1
Walter Roberson
Walter Roberson 2016년 6월 28일
When I read the title I thought you might mean "sparse", and was thinking about how I might do an efficient moving average on sparse data.

댓글을 달려면 로그인하십시오.

채택된 답변

Chad Greene
Chad Greene 2016년 6월 30일
I turned this into a generalized function called scatstat1, which is on the file exchange here.

추가 답변 (2개)

Chris Turnes
Chris Turnes 2017년 3월 9일
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
Walter Roberson 2016년 6월 28일
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.
  댓글 수: 3
Walter Roberson
Walter Roberson 2016년 6월 30일
편집: Walter Roberson 2016년 6월 30일
I wonder if looping pdist2() would be efficient? Eh, it probably just adds unnecessary overhead to a simple Euclidean calculation.
Chad Greene
Chad Greene 2016년 7월 1일
Also adds a Stats Toolbox dependency. I'll have to keep pdist in mind for future applications though. Thanks for the suggestion!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by