필터 지우기
필터 지우기

Mean value of data with irregular intervals

조회 수: 3 (최근 30일)
bobo
bobo 2013년 7월 29일
댓글: Andrew Sandeman 2023년 6월 15일
Hello,
I am trying to obtain average y value for a data set (x,y). However, interval between data points is not regular and mean(y) is off from the expected value. For example,
x = [1 1.1 1.2 1.3 2 3 4 5.1 5.2 5.3 5.4]; y = [0 0.1 0.2 0.3 1 2 1 0.3 0.2 0.1 0];
Because x,y are more densely lying in x = [0 1] and [5 6] intervals, y values in those intervals are weighted more if I take mean(y). Do you know how to avoid this and obtain y value in uniform intervals?
Thanks, Bobo

채택된 답변

Iain
Iain 2013년 7월 29일
Why not just take a weighted mean?
This method ignores y(1) - you could change it to ignore y(end), or add info and inclue it.
totalweight = 0;
weightedsum = 0;
for i = 2:numel(x)
weight = x(i)-x(i-1);
weightedsum = weightedsum + weight*y(i);
totalweight = totalweight + weight;
end
meanvalue = weightedsum / totalweight;
  댓글 수: 1
dpb
dpb 2013년 7월 29일
편집: dpb 2013년 7월 30일
d=diff(xx);
m=sum(d.*yy(2:end))/sum(d);
which can also be written as
m=dot(diff(xx),yy(2:end))/sum(d);

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

추가 답변 (1개)

dpb
dpb 2013년 7월 29일
편집: dpb 2013년 7월 29일
One way to weight uniformly...
dx=min(diff(x)); % shortest difference in vector
xi=x(1):dx:x(end); % fill in all values equally
yhat=interp1(x',y',x1'); % and get corresponding y's...
mean(yhat); % obviously can combine steps ...
  댓글 수: 2
bobo
bobo 2013년 7월 30일
Thanks!!
Andrew Sandeman
Andrew Sandeman 2023년 6월 15일
In my opinion this is the correct answer, not the currently accepted answer.
  • This method does not neglect y(1)
  • Samples which are obtained more sparsely are weighted more accurately using this interpolation method

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by