Loren on the Art of MATLAB

Turn ideas into MATLAB

Another Possible Surprise – Ignored NaN Values

Ever have some data that might have some NaN values? And you start doing computations with these data, expecting |NaN|s to propagate... Only to find later, that the |NaN|s only went so far. Here's what's going on, for better or worse.


NaNs Play Different Roles

I know of 3 typical uses of NaN values:

  • to represent missing data
  • result of computational ambiguity (e.g., 0/0)
  • graphics directive to separate line segments in a single vector of point values

Some MATLAB Functions Ignore NaN Values

There are several MATLAB functions that ignore NaN valuess, including

min, max, any, and all Here are a few little examples in action.

any([0 nan])
all([0 nan])
all([1 nan])
any([1 nan])
ans =
ans =
ans =
ans =

There are also a bunch of functions in Statistics Toolbox that ignore |NaN|s, including these:

nanmean, nanmin, nanmax, nancov, nanstd, nansum, nanvar, nanmedian

Why Ignore NaNs?

The reason min, max, any and all ignore NaN values is for the the graphics case - where line segments in a line are separated by NaN, and finding the min and max values is useful for setting axis limits.

How Do You Use NaNs?

Have you found the tools you need to work with your data containing NaN values? Do you need to work around them or do you embrace their presence? Let me know here.

Published with MATLAB® 7.13

  • print
  • send email


To leave a comment, please click here to sign in to your MathWorks Account or create a new one.