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

117 visualizaciones (últimos 30 días)
temp = X;
temp(~isnan(temp)) = 1;
temp(isnan(temp)) = 0;
temp = find(temp);
first_non_NaN_index_of_X = temp(1);

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Structures en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by