Replacing NaN with its succeeding values

1 回表示 (過去 30 日間)
Sowmya MR
Sowmya MR 2016 年 8 月 2 日
回答済み: Andrei Bobrov 2016 年 8 月 2 日
I have an array which should replace the NaN with its succeeding value. For example:
A=[NaN NaN 1 1 0 0 NaN 0 NaN 1 1]
then output should be
B=[1 1 1 1 0 0 0 0 1 1 1].
Can someone please help me with this?

採用された回答

Brendan Hamm
Brendan Hamm 2016 年 8 月 2 日
This is simply a 1-dimensional interpolation using the next method with extrapolation:
A=[NaN NaN 1 1 0 0 NaN 0 NaN 1 1]
x = 1:length(A); % Create a vector of indices
idx = ~isnan(A); % Find the locations of valid data
% Use the good data to interpolate on all the locations in x.
B = interp1(x(idx),A(idx),x,'next','extrap')

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2016 年 8 月 2 日
A=[NaN NaN 1 1 0 0 NaN 0 NaN 1 1];
z = flip(A(:));
x = ~isnan(z);
z = z(x);
B = flip(z(cumsum(x)))';

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by