MATLAB Answers

How can I find the average of several lines whose datasets are different lengths?

조회 수: 12(최근 30일)
Trevor D Clark
Trevor D Clark 2021년 9월 16일
댓글: Trevor D Clark 2021년 9월 16일
I'm able to plot the various datasets altogether thanks to normalizing the x axis values, but I need to find the 'average line' per say.
I'm unable to use a mean function because the datasets are differing lengths, any ideas?
for i = 1:length(plot_limit)
frame = find((inverse_kin(:,1) >= plot_limit(i,1)) & (inverse_kin(:,1) <= plot_limit(i,2)));
x{i} = normalize(inverse_kin(frame,1),'range',[0 100]);
y{i} = inverse_kin(frame,9);
hold on
for i = 1:length(plot_limit)
graph(i) = plot(x{i},y{i});
hold off

채택된 답변

Image Analyst
Image Analyst 2021년 9월 16일
편집: Image Analyst 2021년 9월 16일
I suggest you resample each curve with interp1() so that all your curves are using a common set of x values. Then simply add them up and divide by the number. Something like (untested):
numPoints = 1000; % Whatever resolution you want.
xCommon = linspace(0, 100, numPoints);
ySum = zeros(1, numPoints);
for k = 1 : numberOfCurves
% Somehow get this particular set of x and y
thisx =
thisy =
% Interpolate y so that it's using a common x axis.
yCommon = interp1(thisx, thisy, xCommon);
% Add it in to the other curves.
ySum = ySum + yCommon;
% Divide the sum by the number of curves to get the average curve.
yAverage = ySum / numberOfCurves;

추가 답변(1개)

the cyclist
the cyclist 2021년 9월 16일
One possible solution would be to interpolate (e.g. with spline function) using the interp1 function, to a common grid of points along the x-axis, then take the mean over those points.

Community Treasure Hunt

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

Start Hunting!

Translated by