MATLAB Answers

# How to average plots that differ in x and y data?

13 views (last 30 days)
Samuel on 27 Apr 2017
Answered: Joseph Cheng on 28 Apr 2017
Hello- I currently have three different datasets that signify results of heating up 3 different specimens. Since they were carried in different times, they vary in time and value, but also differ in the number of datapoints it used.
I am trying to plot the average of the three.
Now if the number of datapoints are the same, the method is quite simple- but my case is something like:
dataset a: 2 x 10 dataset dataset b: 2 x 15 dataset dataset c: 2 x 20 dataset
but imagine they signify a temperature rise. This also starts and ends in different values.
Doing some research I also found a exchange based program called plotaverage, but it is a bit confusing for me to find out how to use it, and the example I saw of it being used was for datasets that contained the same amounts of data.
Any help will be greatly appreciated!
thank you s

#### 1 Comment

Star Strider on 27 Apr 2017
It would help to have some or all of your data, or data similar enough to it to be relevant (as a ‘.mat’ file attached here) and a bit better description of what you want to do with it.
Is interpolation an option to create equal-length vectors if that is necessary?

Sign in to comment.

### Answers (1)

Joseph Cheng on 28 Apr 2017
you can do something like
x1 = 0:.01:4;
x2 = 0:.03:5;
x3 = 0:.02:6;
y1 = sin(3*pi*x1);
y2 = sin(3*pi*x2);
y3 = sin(3*pi*x3);
figure,plot(x1,y1,'o',x2,y2,'s',x3,y3,'x')
p(1)=pchip(x1,y1);
p(2)=pchip(x2,y2);
p(3)=pchip(x3,y3);
time = min([x1 x2 x3]):...
min([diff(x1) diff(x2) diff(x3)]):...
max([x1 x2 x3]);
for ind = 1:3
output(ind,:)=ppval(p(ind),time);
end
% cut out unkown time
output(1,find(time>max(x1)))=nan;
output(2,find(time>max(x2)))=nan;
output(3,find(time>max(x3)))=nan;
hold on
plot(time,output',time,nanmean(output),'--')
legend('data1','data2','data3',...
'interp1','interp2','interp3',...
'average sig')
which you'll end up with the interpolation using pchip(), you can take a look at spline() depending on how you want to interpolate. with the set code you can code in to set the interpolated time to the shortest time, or what i show above by setting the interpolated section beyond the known with Nan's for each signal and then use nanmean() to not average them

#### 0 Comments

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!