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

117 visualizzazioni (ultimi 30 giorni)
temp = X;
temp(~isnan(temp)) = 1;
temp(isnan(temp)) = 0;
temp = find(temp);
first_non_NaN_index_of_X = temp(1);

Risposta accettata

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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Structures in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by