How to interpolate only NaN values with one hole?

2 ビュー (過去 30 日間)
Gavin Plume
Gavin Plume 2024 年 3 月 2 日
コメント済み: Dyuman Joshi 2024 年 3 月 4 日
I have a large matrix (23x6939) with many NaN values. I need to interpolate over any gaps that are only one column wide, but leave any gaps that are two or more NaN values wide.
For example,
x=[1 2 4 4 NaN 6 9 8; 12 13 NaN NaN 20 21 24] becomes [1 2 4 4 5 6 9 8; 12 13 NaN NaN 20 21 24].
How can I distinguish between gaps that are only one value and gaps that are more than one value before applying interp1?

採用された回答

Dyuman Joshi
Dyuman Joshi 2024 年 3 月 2 日
fillmissing is a better fit here -
%Note that the data you have provided is not valid
%I have added another element to make it compatible for concatenation
%Sample data
x = [1 2 4 4 NaN 6 9 8; 12 13 NaN NaN 20 21 24 25; 45 NaN NaN NaN 66:69; 3 5 7 11 13 17 19 23]
x = 4x8
1 2 4 4 NaN 6 9 8 12 13 NaN NaN 20 21 24 25 45 NaN NaN NaN 66 67 68 69 3 5 7 11 13 17 19 23
%Fill the gaps less than or equal to MaxGap with linear interpolation,
%along the 2nd dimension
y = fillmissing(x, 'linear', 2, 'MaxGap', 2)
y = 4x8
1 2 4 4 5 6 9 8 12 13 NaN NaN 20 21 24 25 45 NaN NaN NaN 66 67 68 69 3 5 7 11 13 17 19 23
Refer to the MaxGap Name-value Argument section of fillmissing() for information regarding how this feature is defined.
  3 件のコメント
Gavin Plume
Gavin Plume 2024 年 3 月 4 日
Worked like a charm. Thank so much for your help!
Dyuman Joshi
Dyuman Joshi 2024 年 3 月 4 日
You're welcome!

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by