MATLAB: Averaging data with slightly different time values

average

Last week I got data from an experiment with three repeats, and unlike all of the examples in previous experience the times that the samples were collected are slightly different, as an example:
t_1 = [0 1 2 3 4 5], x_1 = [0 10 20 30 40 50]
t_2 = [0.1 1.1 2.1 3.2 4.2 5.4], x_2 = [0 9 19 29 38 40 51]
t_3 = [0 0.9 2 3.1 3.9 4.8], x_3 = [0 11 21 31 39 48]
For the sake of analyzing the data I would like to reduce this data to one set.
I thought of a couple of options: taking time intervals and averaging the data within that interval, or maybe interpolating the data for each data set onto fixed times and then averaging those values. Either of these seem fine, with the latter being preferable. However, the real data set is extensive and I was wondering if there was an easy way of summarizing this data? Currently the only way i could solve it would be with a tonne of if and for statements which would be very tedious, probably wouldn't save much time and may not even work.
Any help would be greatly appriciated!

Best Answer

Hi,
a further approach would be not to manipulate data, but use all the Information the data contains and make a fit function with least suqared minimized coefficients:
t_1 = [0 1 2 3 4 5];
x_1 = [0 10 20 30 40 50];
t_2 = [0.1 1.1 2.1 3.2 4.2 5.4];
x_2 = [0 9 19 29 38 51];
t_3 = [0 0.9 2 3.1 3.9 4.8];
x_3 = [0 11 21 31 39 48];
% combine all data in one set and sort this set by time
A(:,1) = horzcat(t_1,t_2,t_3);
A(:,2) = horzcat(x_1,x_2,x_3);
sortrows(A,1)
% plot the data
plot(A(:,1),A(:,2),'*r')
hold on
% Solve least squares problem
x0 = [1 1];
x_data = A(:,1);
sol = lsqcurvefit(@myfun,x0,A(:,1),A(:,2));
% show results
disp(['y = ', num2str(sol(1)), ' * ', ' x + ' , num2str(sol(2))])
x_new = 0:6;
y_new = sol(1) .* x_new + sol(2);
plot(x_new,y_new)
hold off
% target function to find coefficients
function F = myfun(x,xdata)
F = x(1)*xdata + x(2);
end
This gives you a least squares fit of all the data without manipulating them:
Best regards
Stephan