フィルターのクリア

Interpolation with NaN: how to interpolate a vector so it DOES contract the NaN

2 ビュー (過去 30 日間)
Adam Pigon
Adam Pigon 2017 年 11 月 25 日
コメント済み: Adam Pigon 2017 年 11 月 26 日
Dear All, I have the following problem:
There are the following vectors:
a = [-1, 0.15, 0.7, 0.9, 1.1, 1.98, NaN, NaN, 2.7, 2.9, 3.1, NaN, NaN, NaN],
b = randn(length(a))
x = 0:5
I would like to interpolate x on a:
V = interp1(a,b,x)
Unfortunately, vector a contains NaN, which makes it impossible. I could use
V = interp1(a(~isnan(a)),b(~isnan(a)),x)
but then I get crude interpolation for values, in this case, between 1.98 and 2.7 while they should in fact do not exist, i.e. they should get NaN. How to do it ? How can I 'transfer' NaN values from vector a to vector x so that all x values that are next to NaN in a turn into NaNs and I can use interp1(a(~isnan(a)),b(~isnan(a)),x) getting proper results ?
Any help would be appreciated!

回答 (1 件)

Matt J
Matt J 2017 年 11 月 26 日
If you mark missing data by putting NaNs in "b" instead of in "a", then you will be able to interpolate freely.
  3 件のコメント
Matt J
Matt J 2017 年 11 月 26 日
編集済み: Matt J 2017 年 11 月 26 日
You should restore the NaNs in "a" to whatever values they had before you overwrote them with NaNs.
Adam Pigon
Adam Pigon 2017 年 11 月 26 日
if it were that easy I would have already done it :) These values do not exist. Fortunately, there is a solution to this problem: you can perform a linear interpolation of these values.
a = [1 2 3 NaN NaN 5 NaN 6 7 NaN 8];
t = 1:length(a);
d = interp1(t(~isnan(a)),a(~isnan(a)),t);
Then it is enough to give vector b NaN's using indices from vector a and perform the interpolation using interp1. Problem solved!

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

カテゴリ

Help Center および File ExchangeInterpolation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by