Estimate length with out NaN's

168 views (last 30 days)
George on 11 Mar 2015
Commented: Star Strider on 11 Mar 2015
I use the length command to see the length of some of my variables, although they include NaN's inside by a previous script I used so as to fill them and compare them Is there a way to find the length of the data non-taking into account the NaN?
I tried nanlength , as you can use nanmean etc. although it does not like it
it gives me this error
Undefined function 'nanlength' for input arguments of type 'double'.
  1 Comment
Stephen23 on 11 Mar 2015
Edited: Stephen23 on 11 Mar 2015
Keep in mind that length does not give the number of elements in an array: for this you should use numel .

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 11 Mar 2015
This works:
nonan_len = length(x(~isnan(x)));

Sign in to comment.

More Answers (1)

Giorgos Papakonstantinou
Giorgos Papakonstantinou on 11 Mar 2015
If x is your vector.
YourLength = length(x) - sum(isnan(x))


Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!