Loren on the Art of MATLAB

Turn ideas into MATLAB

When is a Numeric Result Not a Number?

Quick answer: when the result is aNaN.

Contents

NaNs in Arithmetic

MATLAB has followed theIEEE 754: Standard for Binary Floating-Point Arithmeticfor doing arithmetic from the early days, and included tools and functionality to achieve similar results on machines that did not comform to the IEEE standards, e.g., Digital Equipment Corporations VAX computers. In addition, we made choices in MATLAB to allow division by 0 rather than causing an error on machine architectures that could handle this behavior (allowed in the IEEE standard). In allowing division by 0, however, we not only encounter cases in which we divide nonzero positive numbers and negative numbers by 0 (yieldingInfand-Infrespectively, but we also encounter computations that result in undefined mathematical outcomes such as

inf-inf
ans = NaN
0/0
Warning: Divide by zero. ans = NaN

NaNs as Placeholders

In addition to allowingNaNas the result of numeric operations, we've also encouraged their use for marking information such as missing data. If we were gathering some data for some specific times, and missed collecting one of the values, we might end up with data that look like this:

ts = 0:5; vals = [0.2 4.3 -1.6 NaN -2.0 1.2] plot(ts,vals,'m*-')
vals = Columns 1 through 5 0.2000 4.3000 -1.6000 NaN -2.0000 Column 6 1.2000

How to Find or Compare to NaN

According to the IEEE standard,NaNis not a number and not equal to anything, including itself. So when looking forNaNvalues in an array, you can't do the "obvious"

瓦尔斯= =南
ans = 0 0 0 0 0 0

As you can see, the comparison is alwaysfalse. So how do we look for them? Depending on what you are doing, you will find these functions helpful:

虽然莫uthful to say or write,isequalwithequalnansallows you to carry out comparisons between arrays matching values and shape, without allowing scalar expansion.

Another way to make look forNaNis frequently cited on the MATLAB newsgroup. Seethis thread and commentsby Paul Bodin for an example. The idea here is that since

NaN == NaN
ans = 0

always produces afalseresult, we can find NaNs simply by looking for places in which the value doesn't equal itself. These are NaNs, and other values are not.

ourNaNs = vals(vals~=vals)
ourNaNs = NaN
nonNaNs = vals(vals==vals)
nonNaNs = 0.2000 4.3000 -1.6000 -2.0000 1.2000

My preference is to useisnaninstead because I find the code much more readable when I revisit it later. However, in some cases, depending on the amount of NaN values (and perhaps other considerations), the constructs just above are sometimes faster. Here are a couple of additional links to newsgroup threads talking more about this.

NaN issues

Some users find themselves experiencing collisions betweenNaNfor missing data vs.NaNas a result of undefined numerical operations.

  • 你遇到过这种碰撞吗?
  • Do you find yourself wishing for more placeholder values?
  • What else would you like to useNaNor something similar for?

Let's hear your thoughts.


Published with MATLAB® 7.2

|

Comments

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