How to eliminate Repeated NaNs in a array?

2 ビュー (過去 30 日間)
Pappu Murthy
Pappu Murthy 2019 年 10 月 28 日
コメント済み: Adam Danz 2019 年 10 月 28 日
I have two vectors X, and Y; both will have NaNs present in them.
If X has a NaN, Y will have a NaN as well at the same location.
I would like to squeeze repepated NaNs to just one NaN.
For e.g. if X = [1,2.3, NaN, NaN, NaN, 2, 1].. then
X should be replaced by X=[1,2,3,NaN,2,1] and
Y also should reflect the same.
How would I achieve it?

採用された回答

Adam Danz
Adam Danz 2019 年 10 月 28 日
% Demo data
x = [1:5 nan nan nan nan nan nan, 7:10, nan, 12, nan, 13:15, nan nan nan 19 ];
keepIdx = ~isnan(x); % keep all non-nans
keepIdx(logical([1,diff(~keepIdx)])) = true; % keep first of consecutive nans
x = x(keepIdx); % remove unwanted values from x
y = y(keepIdx); % remove unwanted values from y
  2 件のコメント
Pappu Murthy
Pappu Murthy 2019 年 10 月 28 日
Thanks for helping me out. It woked exactly the way i wanted.
Adam Danz
Adam Danz 2019 年 10 月 28 日
Glad I could help!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by