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

116 vues (au cours des 30 derniers jours)
Mr M.
Mr M. le 3 Juil 2018
Commenté : OCDER le 3 Juil 2018
temp = X;
temp(~isnan(temp)) = 1;
temp(isnan(temp)) = 0;
temp = find(temp);
first_non_NaN_index_of_X = temp(1);

Réponse acceptée

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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by