MATLAB: Finding an average curve

average curve

Hi!
Just wondering does anybody know the best way to find the average curve from a group of curves using matlab?
I have been using excel to first find the trendline of each curve. I have then subbed in x=1,2,3 etc to find the corresponding y values for each graph. I then average all the y values and plot them against my x values. It is a very very long and tedious method as i have lots and lots of data.
I hope somebody knows a better way to do it in matlab and is willing to share! I should mention i am a matlab newbie!
Thanks
Siobhan

Best Answer

When you say "find the trendline of each curve" I assume you mean that you have several sets of (x,y) data and you're doing a (linear?) fit to each of those sets? If so, forget Excel and just do the whole lot in MATLAB!
% Make some pretend data
% Each row is a data set
x = (0:5)';
y = [3 + 2*x,3.2 + 1.6*x,2.6 + 2.2*x] + 0.2*randn(6,3);
% Make design matrix
M = [x,x.^0];
% Solve for coefficients
C = M\y;
% Evaluate fits
yfit = M*C;
% Take average of fits
avgyfit = mean(yfit,2);
% See the results
plot(x,y,'x')
hold on
plot(x,yfit)
plot(x,avgyfit,'k','linewidth',2)