Loren on the Art of MATLAB

Turn ideas into MATLAB

Note

Loren on the Art of MATLAB has been archived and will not be updated.

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.

Contents

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 =
     0
ans =
     0
ans =
     1
ans =
     1

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

评论

要发表评论,请点击 此处 登录到您的 MathWorks 帐户或创建一个新帐户。