Is there a simpler way to find the index of the first non NaN value in a vector?

118 ビュー (過去 30 日間)
Mr M.
Mr M. 2018 年 7 月 3 日
コメント済み: OCDER 2018 年 7 月 3 日
temp = X;
temp(~isnan(temp)) = 1;
temp(isnan(temp)) = 0;
temp = find(temp);
first_non_NaN_index_of_X = temp(1);

採用された回答

OCDER
OCDER 2018 年 7 月 3 日
編集済み: OCDER 2018 年 7 月 3 日
X= [NaN NaN 1 2 3 4 5];
first_non_NaN_index_of_X = find(~isnan(X), 1);
  2 件のコメント
Mr M.
Mr M. 2018 年 7 月 3 日
Thanks, and what about last non NaN? I have to use fliplr, or not necessary?
OCDER
OCDER 2018 年 7 月 3 日
No need as the find function has a last one search feature.
last_non_NaN_index_of_X = find(~isnan(X), 1, 'last')

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by