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

5 views (last 30 days)
Trevor D Clark on 16 Sep 2021
Commented: Trevor D Clark on 16 Sep 2021
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);
end
figure;
hold on
for i = 1:length(plot_limit)
graph(i) = plot(x{i},y{i});
end
hold off Image Analyst on 16 Sep 2021
Edited: Image Analyst on 16 Sep 2021
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;
end
% Divide the sum by the number of curves to get the average curve.
yAverage = ySum / numberOfCurves;
Trevor D Clark on 16 Sep 2021
This method worked really well, thanks for your help!

the cyclist on 16 Sep 2021
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.