简体   繁体   中英

Interpolation of a vector in MATLAB depending on changes in another vector

I have two column vectors 'A' and 'B'. The first column in both vectors is time.

A =
35.2985    5.7057
35.2991    5.7098
35.2997    5.6880
35.3004    5.6739
35.3010    5.7140
35.3016    6.0141
35.3022    6.3620
35.3029    6.4793
35.3035    6.4663
35.3041    6.4665
35.3047    6.4646
35.3053    6.4844
35.3060    6.4743
35.3066    6.4865
35.3072    6.4878
35.3078    6.4975
35.3085    6.4952
35.3091    6.4958
35.3097    6.4734
35.3103    6.4082
35.3109    6.3767
35.3116    6.3786
35.3122    6.3796
35.3128    6.5867
35.3134    7.0733
35.3141    7.3427
35.3147    7.3238
35.3153    7.3093
35.3159    7.3188
35.3166    7.3436

B =
35.2985    1.0500
35.3053    1.0600
35.3085    1.0400
35.3166    1.0700

By doing following plot

plotyy(A(:,1),Binter,A(:,1),A(:,2))

It is visible that 'A' has three different levels. For each level of 'A' there is correspond 'B' value. Now I required to get interpolated values of B(:,2) based on time A(:,1). 'Binter' is the column of length 'A'.

Binter= interp1(B(:,1),B(:,2),A(:,1),'linear')
Binter =
NaN
1.0509
1.0518
1.0527
1.0537
1.0546
1.0555
1.0564
1.0573
1.0582
1.0591
1.0597
1.0558
1.0519
1.0481
1.0442
1.0403
1.0421
1.0444
1.0468
1.0491
1.0514
1.0537
1.0560
1.0583
1.0606
1.0629
1.0652
1.0675
1.0698

But I required to start to do only interpolation for the periond in which A(:,2) is changing rapidly [diff(A(:,2))>=0.1]. The rest should not do any interpolation but consider the orginal values. The out put should be something like this

Required_Output =
   NaN
1.0500
1.0500
1.0500
1.0500
1.0525
1.0550
1.0575
1.0600
1.0600
1.0600
1.0600
1.0558
1.0519
1.0481
1.0442
1.0400
1.0400
1.0400
1.0400
1.0400
1.0400
1.0400
1.0475
1.0550
1.0625
1.0700
1.0700
1.0700
1.0700

In the 'Required_Output' there is no interpolation done for corresponding 'A(:,2)' when changes in 'A(:,2)' were smaller or only one point in 'B(:,2)' was available in the different levels of 'A(:,2)'. The single available point in 'B(:,2)'is taken without any interpolation.The interpolation started until changes 'A(:,2)' stays larger than defined or more than one point available in 'B(:,2)' for the different levels in 'A(:,2)'. Thank you for your suggestion/expertise.

The first thing should be quite simple: just define the steps of interpolation

B = [100.3716    1.0500;  100.3894    1.0600];
step = 100.3741:0.0006:100.3869; %true for you actual matrix A
Binter = interp1(B(:,1),B(:,2),step,'linear');

The second question can be reversed (it is easier): first calculate Binter , then check if A(i,2) is NOT changing rapidly. Of course the difference can be defined between A(i,2) and A(i-1,2). I think diff(A(:,2))>=0.1 is not a good line code, Try this instead (not fully tested because I didn't want to set A as a matrix with every single semicolumn)

N = size(Binter,2);
C = diff(A);
for i=1:N-1
    if (C(i,2)<0.1) && (i < N/2)
      Binter(i) = B(1,2);
    else if (C(i,2)<0.1) && (i >= N/2) 
         Binter(i) = B(2,2);
        end
    end
end
Binter(N) = B(2,2);
disp('Required Output =');
disp(Binter');

Whit the assumption there are at least some points in the middle of Binter that have not to be changed and these points are only in the middle of Binter . In order not to fail with C(N,2) that it will cause "out of bounds index" I choose to have N-1 as a limit in the loop and to add the last changing line Binter(N) = B(2,2);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM