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

106 views (last 30 days)
temp = X;
temp(~isnan(temp)) = 1;
temp(isnan(temp)) = 0;
temp = find(temp);
first_non_NaN_index_of_X = temp(1);

Accepted Answer

OCDER
OCDER on 3 Jul 2018
Edited: OCDER on 3 Jul 2018
X= [NaN NaN 1 2 3 4 5];
first_non_NaN_index_of_X = find(~isnan(X), 1);
  2 Comments
OCDER
OCDER on 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')

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!