Interpolating missing values according to time vector

2 ビュー (過去 30 日間)
David E.S.
David E.S. 2021 年 2 月 22 日
コメント済み: David E.S. 2021 年 2 月 22 日
Hi!
I want to interpolate the missing values of this vector:
x=[2 6 8 10 NaN NaN NaN NaN 21 59 NaN 12 80];
but, instead of using the intermediate values in each case, I want to interpolate according another vector of "serial times", for example:
time=[738097.24, 738097.253, 738097.261, 738097.4, 738097.53, 738097.69, 738097.81, 738097.8129, 738097.91, 738098.0, 738098.3, 738098.49, 738098.79];
That means, the values between x(4) and x(9) must respect the sequence given in the time vector.
Is there any form to do that automatically?
Thanks!
  1 件のコメント
dpb
dpb 2021 年 2 月 22 日
I have no idea what you mean or expect the result to be???

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

採用された回答

Cris LaPierre
Cris LaPierre 2021 年 2 月 22 日
Sure. You just have to separate your knowns and unknowns.
x=[2 6 8 10 NaN NaN NaN NaN 21 59 NaN 12 80]';
time=[738097.24, 738097.253, 738097.261, 738097.4, 738097.53, 738097.69, 738097.81, 738097.8129, 738097.91, 738098.0, 738098.3, 738098.49, 738098.79];
% Create index of known values
indKnown = ~isnan(x);
% interpolate to obtain values of unknown values
x(~indKnown) = interp1(time(indKnown),x(indKnown),time(~indKnown))
x = 13×1
2.0000 6.0000 8.0000 10.0000 12.8039 16.2549 18.8431 18.9057 21.0000 59.0000
  1 件のコメント
David E.S.
David E.S. 2021 年 2 月 22 日
That's exactly what I want! Thanks!!

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

その他の回答 (1 件)

Bob Thompson
Bob Thompson 2021 年 2 月 22 日
I don't know if there is a single command that would do what you're asking, though others might know of one.
A fairly concise way of finishing the array though is the following:
x(isnan(x)) = interp1(time(~isnan(x)),x(~isnan(x)),time(isnan(x)));
  1 件のコメント
David E.S.
David E.S. 2021 年 2 月 22 日
Thanks!! I can't accept more than one anwser, but the code is just perfect and fulfills its function!!

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

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by