Filter löschen
Filter löschen

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

121 Ansichten (letzte 30 Tage)
temp = X;
temp(~isnan(temp)) = 1;
temp(isnan(temp)) = 0;
temp = find(temp);
first_non_NaN_index_of_X = temp(1);

Akzeptierte Antwort

OCDER
OCDER am 3 Jul. 2018
Bearbeitet: OCDER am 3 Jul. 2018
X= [NaN NaN 1 2 3 4 5];
first_non_NaN_index_of_X = find(~isnan(X), 1);
  2 Kommentare
Mr M.
Mr M. am 3 Jul. 2018
Thanks, and what about last non NaN? I have to use fliplr, or not necessary?
OCDER
OCDER am 3 Jul. 2018
No need as the find function has a last one search feature.
last_non_NaN_index_of_X = find(~isnan(X), 1, 'last')

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by