How to interpolate when we have two different sizes

4 views (last 30 days)
Hi everyone,
Someone would be kind enough to help me how to do an interpolation when we have two different sizes being A and B.
I will do interpolation A and B then concatenate the result as follows interpolation_A_B= [interpolation_A, interpolation_B].
Here is my code:
%% Data
A=[0,-2.24489741646952,-4.62733420618208,-6.95719767650665,-9.01707651070867,-10.6031942911103,-11.5925433634751,-11.8888513183929,-11.4910074241661,-10.3269616567176,-8.37131286980876,-5.73036557765802,-2.73380439351284,0.0658350852258175,2.24268430772633,3.60945691919934,4.23841606693665,4.38617345954930,4.33237937220543,4.24478638091837,4.11981257851839,3.91272625898743,3.64047136322393,3.40598831808841,3.35826429186336,3.50296413009803,3.66772774367983,3.56377296314676,2.91695995975226,1.61068736575304];
temps_A=[10,56,103,149,196,242,289,335,382,428,475,521,568,614,661,707,754,800,847,893,940,986,1033,1079,1126,1172,1219,1265,1312,1358];
B=[0,0.266260173065289,-0.0707528123775336,-1.10727656343698,-2.67932011084229,-4.49683089873686,-6.28901889839069,-7.86823684856092,-9.10292224756012,-9.93528552429639,-10.3943919840282,-10.6200553018127,-10.6751851242251,-10.4946119723262,-9.93187689931717,-8.79370001488421,-7.21878022731030,-5.48155200603622,-3.92374944116942,-2.83303212357369,-2.34292583956979,-2.31255526161581,-2.51444122441427,-2.69362744473124,-2.68977697021639,-2.43432161929766,-1.93071631270870,-1.25144435000955,-0.517481281517809,-1.93071631270870,-1.25144435000955,-0.517481281517809];
temps_B=[10,41,72,104,135,166,198,229,260,292,323,354,386,417,448,480,511,542,574,605,636,668,699,731,762,793,825,856,887,918,949,980,1011];
%% interpolation A
time_interpolation_A=min(temps_A):1:max(temps_A);
A_interpolation = interp1(temps_A,A,time_interpolation_A);
%% interpolation B
time_interpolation_B=min(temps_B):1:max(temps_B);
B_interpolation = interp1(temps_B,B,time_interpolation_B);
%% concatenate interpolation A & B
Interpolation_A_B=[A_interpolation,B_interpolation];

Accepted Answer

Star Strider
Star Strider on 2 Jun 2021
A=[0,-2.24489741646952,-4.62733420618208,-6.95719767650665,-9.01707651070867,-10.6031942911103,-11.5925433634751,-11.8888513183929,-11.4910074241661,-10.3269616567176,-8.37131286980876,-5.73036557765802,-2.73380439351284,0.0658350852258175,2.24268430772633,3.60945691919934,4.23841606693665,4.38617345954930,4.33237937220543,4.24478638091837,4.11981257851839,3.91272625898743,3.64047136322393,3.40598831808841,3.35826429186336,3.50296413009803,3.66772774367983,3.56377296314676,2.91695995975226,1.61068736575304];
temps_A=[10,56,103,149,196,242,289,335,382,428,475,521,568,614,661,707,754,800,847,893,940,986,1033,1079,1126,1172,1219,1265,1312,1358];
B=[0,0.266260173065289,-0.0707528123775336,-1.10727656343698,-2.67932011084229,-4.49683089873686,-6.28901889839069,-7.86823684856092,-9.10292224756012,-9.93528552429639,-10.3943919840282,-10.6200553018127,-10.6751851242251,-10.4946119723262,-9.93187689931717,-8.79370001488421,-7.21878022731030,-5.48155200603622,-3.92374944116942,-2.83303212357369,-2.34292583956979,-2.31255526161581,-2.51444122441427,-2.69362744473124,-2.68977697021639,-2.43432161929766,-1.93071631270870,-1.25144435000955,-0.517481281517809,-1.93071631270870,-1.25144435000955,-0.517481281517809];
temps_B=[10,41,72,104,135,166,198,229,260,292,323,354,386,417,448,480,511,542,574,605,636,668,699,731,762,793,825,856,887,918,949,980];
Data = [size(A); size(B)]
Data = 2×2
1 30 1 32
Create new temperature vectors from the existing vectors, and interpolate.
NewLength = 50;
NewAt = linspace(min(temps_A), max(temps_A), NewLength);
Anew = interp1(temps_A, A, NewAt)
Anew = 1×50
0 -1.3426 -2.7021 -4.0966 -5.4904 -6.8838 -8.0994 -9.2437 -10.1922 -10.9314 -11.5105 -11.7446 -11.8455 -11.6126 -11.1584 -10.4623 -9.4048 -8.2178 -6.6384 -4.9848 -3.2308 -1.5340 0.1225 1.3967 2.5174 3.3348 3.8539 4.2220 4.3228 4.3773
NewBt = linspace(min(temps_B), max(temps_B), NewLength);
Bnew = interp1(temps_B, B, NewBt)
Bnew = 1×50
0 0.1700 0.1729 -0.0424 -0.6274 -1.3598 -2.3637 -3.4750 -4.6294 -5.7381 -6.7964 -7.8048 -8.6071 -9.2940 -9.8089 -10.1565 -10.4216 -10.5657 -10.6413 -10.6745 -10.5592 -10.3364 -9.9771 -9.3163 -8.5345 -7.5288 -6.4514 -5.3603 -4.3967 -3.5690
figure
plot(temps_A, A, '.-b')
hold on
plot(NewAt, Anew, '.r')
hold off
grid
legend('Original', 'Interpolated', 'Location','best')
title('A')
figure
plot(temps_B, B, '.-b')
hold on
plot(NewBt, Bnew, '.r')
hold off
grid
legend('Original', 'Interpolated', 'Location','best')
title('B')
legend('Original', 'Interpolated', 'Location','best')
.
  4 Comments

Sign in to comment.

More Answers (1)

J Chen
J Chen on 2 Jun 2021
temps_B and B have different size. They need to be the same size.
  1 Comment
Crocola Cool
Crocola Cool on 2 Jun 2021
# Hi J Chen
yes I was mistaken, here is the modified code.
When concatenating, I would like to have interpolation A in column and the same for interpolation B.
clear all
clc
A=[0,-2.24489741646952,-4.62733420618208,-6.95719767650665,-9.01707651070867,-10.6031942911103,-11.5925433634751,-11.8888513183929,-11.4910074241661,-10.3269616567176,-8.37131286980876,-5.73036557765802,-2.73380439351284,0.0658350852258175,2.24268430772633,3.60945691919934,4.23841606693665,4.38617345954930,4.33237937220543,4.24478638091837,4.11981257851839,3.91272625898743,3.64047136322393,3.40598831808841,3.35826429186336,3.50296413009803,3.66772774367983,3.56377296314676,2.91695995975226,1.61068736575304];
temps_A=[10,56,103,149,196,242,289,335,382,428,475,521,568,614,661,707,754,800,847,893,940,986,1033,1079,1126,1172,1219,1265,1312,1358];
B=[0,0.266260173065289,-0.0707528123775336,-1.10727656343698,-2.67932011084229,-4.49683089873686,-6.28901889839069,-7.86823684856092,-9.10292224756012,-9.93528552429639,-10.3943919840282,-10.6200553018127,-10.6751851242251,-10.4946119723262,-9.93187689931717,-8.79370001488421,-7.21878022731030,-5.48155200603622,-3.92374944116942,-2.83303212357369,-2.34292583956979,-2.31255526161581,-2.51444122441427,-2.69362744473124,-2.68977697021639,-2.43432161929766,-1.93071631270870,-1.25144435000955,-0.517481281517809,-1.93071631270870,-1.25144435000955,-0.517481281517809];
temps_B=[10,41,72,104,135,166,198,229,260,292,323,354,386,417,448,480,511,542,574,605,636,668,699,731,762,793,825,856,887,918,949,980];
time_interpolation_A=min(temps_A):1:max(temps_A);
A_interpolation = interp1(temps_A,A,time_interpolation_A);
time_interpolation_B=min(temps_B):1:max(temps_B);
B_interpolation = interp1(temps_B,B,time_interpolation_B);
Interpolation_A_B=[A_interpolation',B_interpolation'];

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!