MATLAB: Plot a mean line of 4 lines

cell arraysmean

Hi all,
I would like to plot a mean line of 4 lines. The arrays of these 4 lines are not the same length, but they cover the same values on the x-axis. Can somebody help me?
Thanks in advance,
Christa

Best Answer

Let s1,s2,s3,s4 be your data. It has (x,y) values. Do interpolation to get them to same size first.
N = 500 ; % put your value
% specimen1
x1 = s1(:,1) ; y1 = s1(:,2) ;
x1i = linspace(min(x1),max(x1),N);
y1i = interp1(x1,y1,x1i) ;
% specimen2
x2 = s2(:,1) ; y2 = s2(:,2) ;
x2i = linspace(min(x2),max(x2),N);
y2i = interp1(21,21,x2i) ;
% specimen3
x3 = s3(:,1) ; y3 = s3(:,2) ;
x3i = linspace(min(x3),max(x3),N);
y3i = interp1(x3,y3,x3i) ;
% specimen4
x4 = s4(:,1) ; y4 = s4(:,2) ;
x4i = linspace(min(x4),max(x4),N);
y4i = interp1(x4,y4,x4i) ;
% Get average
xavg = (x1i+x2i+x3i+x4i)/4 ;
yavg = (y1i+y2i+y3i+y4i)/4 ;
plot(xavg,yavg) ;