Combining 3 curves into 1 average

Hi, I am trying to combine 3 curves into 1 average curve (by taking the mean of the 3 curves). As far as I understand (by reading other questions on this site) I need to use interp1, but I am not sure how to create the 'query points' (that I called n). So far my code looks like this:
n = linspace(???);
yyS = interp1(timeS(:),Y2S(:),n);
yyC = interp1(timeC(:),Y2C(:),n);
yyA = interp1(timeA(:),Y2A(:),n);
avg = mean([yyS, yyC, yyA],3);
figure(10);
plot(avg)
The data has the following dimensions:
timeS & Y2S: 345x1
timeC & Y2C: 336x1
timeA & Y2A: 335x1
Any help would be greatly appreciated!!

 採用された回答

Ameer Hamza
Ameer Hamza 2020 年 6 月 10 日
編集済み: Ameer Hamza 2020 年 6 月 10 日

0 投票

Create 'n' like this
min_n = min([timeS; timeC; timeA])
max_n = max([timeS; timeC; timeA])
n = linspace(min_n, max_n, 300).'; % choose any appropriate number instead of 300
yyS = interp1(timeS(:),Y2S(:),n);
yyC = interp1(timeC(:),Y2C(:),n);
yyA = interp1(timeA(:),Y2A(:),n);
avg = mean([yyS, yyC, yyA], 2); % second input should be 2 (not 3)
figure(10);
plot(n, avg)

6 件のコメント

H Lange
H Lange 2020 年 6 月 10 日
How do I know which number to use? I tried running your code but the plot still doesn't show up...
Ameer Hamza
Ameer Hamza 2020 年 6 月 10 日
These lines will extract minimum and maximum numbers from 3 vectors and then create a new vector using those values.
Can you attach the variables in a .mat file?
H Lange
H Lange 2020 年 6 月 10 日
I've never made a .mat file before, so I hope I did this right :)
Ameer Hamza
Ameer Hamza 2020 年 6 月 10 日
Yes, the .mat file is correct. There was a little type in my answer. Run the updated answer now.
H Lange
H Lange 2020 年 6 月 10 日
Thank you so much!!
Ameer Hamza
Ameer Hamza 2020 年 6 月 10 日
I am glad to be of help!

サインインしてコメントする。

その他の回答 (1 件)

Stephan
Stephan 2020 年 6 月 10 日

0 投票

Here is a simple example:
% fantasy times
times1 = 1:10
times2 = 1:2:11
% fantasy values
y1 = 2 * (1:10)
y2 = 4 * (1:2:11)
% interpolate, query points should be longest time frame
y2_new = interp1(times2,y2,times1)
% Concatenate the vectors to calculate the mean for every pair
y_all = [y1; y2_new];
% get the mean
y_mean = mean(y_all)

2 件のコメント

H Lange
H Lange 2020 年 6 月 10 日
I'm not sure I know how to apply this example to my code. I tried the following, but that doesn't seem to work..
yyS = interp1(timeS,Y2S,timeS);
yyC = interp1(timeC,Y2C,timeS);
yyA = interp1(timeA,Y2A,timeS);
yS = [Y2S; yyS];
yC = [Y2S; yyC];
yA = [Y2S; yyA];
avg = mean([yS,yC,yA]);
Stephan
Stephan 2020 年 6 月 10 日
It is a simple example that you can walk through to understand what happens. I think understanding the way it works and the transfer this knowledge on your problem is easier on the long term than just copy and paste something, that you do not understand.

サインインしてコメントする。

カテゴリ

製品

リリース

R2019b

質問済み:

2020 年 6 月 10 日

コメント済み:

2020 年 6 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by