Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
NaN2: Another NaN, an NaN alternative, two types of NaN?

Subject: NaN2: Another NaN, an NaN alternative, two types of NaN?

From: David Painter

Date: 12 Mar, 2009 07:46:03

Message: 1 of 9

Hello,

I never thought I would find myself asking this, but is there another NaN? I have two sources of NaN values in my program, "incalculable" NaNs (e.g., the answer of NaN - x) and "outlier" NaNs. I want to be able to see them at a glance in an array.

I thought about using infinity (inf) for the outlier NaNs, but there are no infmean and infstd functions to complement nanmean and nanstd. I really don't want to be creating code to avoid this problem. Is something like the following available?

a = [1 2 3 NaN 3 4 5 NaN2];

If not, I would appreciate any one-line solutions: e.g., if(any(inf), ignore it, and nanmean(whatever remains). Inelegant to say the least.

All the best,

D

Subject: NaN2: Another NaN, an NaN alternative, two types of NaN?

From: Roger Stafford

Date: 12 Mar, 2009 08:23:52

Message: 2 of 9

"David Painter" <david.ross.painter@gmail.com> wrote in message <gpaejq$heu$1@fred.mathworks.com>...
> Hello,
>
> I never thought I would find myself asking this, but is there another NaN? I have two sources of NaN values in my program, "incalculable" NaNs (e.g., the answer of NaN - x) and "outlier" NaNs. I want to be able to see them at a glance in an array.
>
> I thought about using infinity (inf) for the outlier NaNs, but there are no infmean and infstd functions to complement nanmean and nanstd. I really don't want to be creating code to avoid this problem. Is something like the following available?
>
> a = [1 2 3 NaN 3 4 5 NaN2];
>
> If not, I would appreciate any one-line solutions: e.g., if(any(inf), ignore it, and nanmean(whatever remains). Inelegant to say the least.
>
> All the best,
>
> D

  The IEEE 754 standard allows for many different NaNs if desired in a system. Any number that starts with hex 7FF... or FFF... and is followed by at least one additional non-zero bit is considered a NaN in the standard.

  In matlab I have found only two different NaNs on my ancient system, 7FF8000000000000 and 7FF8012000000000. The first of these indicates such things as inf-inf, 0*inf, 0/0, and inf/inf. The second occurs apparently only with rem(x,0). I am not sure why this particular operation was selected for a different NaN, but if it still works that way, you could use this for your purposes. However, you do have the problem of recognizing the difference afterwards. At the moment I cannot think of a way to do it.

Roger Stafford

Subject: NaN2: Another NaN, an NaN alternative, two types of NaN?

From: Sebastian H?lz

Date: 12 Mar, 2009 08:26:02

Message: 3 of 9

Just generate a second logical index containing your outliers:

a = [1 2 3 nan 5 6 n -1000 nan 10]; % here the nan's are the "uncalculatables"
uncalcs = isnan(a); % This is the index of "uncalculatables"
outliers = a<0 & a>100; % ... and the index of outliers
a(outliers)=nan; % Set outliers to nan

You can now do your "nanmean" and "nanstd", which are by the way not part of the Matlab-standard (at least not in Matlab 7.5), and will still know the caracteristics of your data. Alternatively to these non-standard-functions, you can quickly calculate mean and standard with:

mean_a = mean(a(~isnan(a)));
std_a = std(a(~isnan(a)));

Sebastian

Subject: NaN2: Another NaN, an NaN alternative, two types of NaN?

From: Roger Stafford

Date: 12 Mar, 2009 08:29:01

Message: 4 of 9

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gpagqo$b9e$1@fred.mathworks.com>...
> ......
> ...... However, you do have the problem of recognizing the difference afterwards. At the moment I cannot think of a way to do it.
> ......

... except of course by using 'format hex'.

Roger Stafford

Subject: NaN2: Another NaN, an NaN alternative, two types of NaN?

From: Bruno Luong

Date: 12 Mar, 2009 09:04:04

Message: 5 of 9

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gpah4d$183$1@fred.mathworks.com>...
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gpagqo$b9e$1@fred.mathworks.com>...
> > ......
> > ...... However, you do have the problem of recognizing the difference afterwards. At the moment I cannot think of a way to do it.
> > ......
>
> ... except of course by using 'format hex'.
>
> Roger Stafford

Practical implementation of Roger's remark

% function to generate an NaN of specific type 1, 2, ..., 255
nant = @(type) typecast(uint8([type 0 0 0 0 0 248 255]),'double');

% Function which get the type, 0 if Matlab double or NaN
gettype = @(x) isnan(x).*double(subsref(typecast(x,'uint8'), ...
                                                               substruct('()',{1})));

x1 = nant(1)
x2 = nant(2)

gettype(x1)
gettype(x2)

Bruno

Subject: NaN2: Another NaN, an NaN alternative, two types of NaN?

From: James Tursa

Date: 12 Mar, 2009 09:04:04

Message: 6 of 9

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gpah4d$183$1@fred.mathworks.com>...
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gpagqo$b9e$1@fred.mathworks.com>...
> > ......
> > ...... However, you do have the problem of recognizing the difference afterwards. At the moment I cannot think of a way to do it.
> > ......
>
> ... except of course by using 'format hex'.
>
> Roger Stafford

Timing is everything ... I was just looking into this topic because of some negative zero behavior I ran into in Fortran and was looking to see how MATLAB handled it, and the NaN patterns as well.

Interestingly, format hex will reveal the NaN pattern difference, but num2hex will not. For example, this simple mex routine produces a NaN pattern with all bits set:

#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    unsigned long *ulp;
    plhs[0] = mxCreateDoubleScalar(0.0);
    ulp = mxGetData(plhs[0]);
    *ulp++ = 0xFFFFFFFFu;
    *ulp = 0xFFFFFFFFu;
}

When you look at the results of this mex function with format hex, you get:

ans =
   ffffffffffffffff

But when you do a num2hex on this same value, you get:

ans =
fff8000000000000

So MATLAB does some type of value conversion for the num2hex function and always produces the same default NaN pattern for display regardless of the actual underlying NaN pattern, whereas the format hex just does a hex conversion of the raw bits apparently. The same type of behavior can be seen for negative zero. You can see that MATLAB will produce it using format hex, but a normal display will not reveal it, nor will a num2hex call:

>> -0
ans =
   8000000000000000
>> num2hex(-0)
ans =
0000000000000000

I am currently working on a suite of functions to handle sign bits directly and different NaN patterns ... getting, setting, comparing bit patterns directly, etc. Will probably upload it to the FEX sometime this weekend so should be available sometime early next week.

James Tursa

Subject: NaN2: Another NaN, an NaN alternative, two types of NaN?

From: James Tursa

Date: 12 Mar, 2009 09:08:01

Message: 7 of 9

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gpagqo$b9e$1@fred.mathworks.com>...
>
> In matlab I have found only two different NaNs on my ancient system, 7FF8000000000000 and 7FF8012000000000. The first of these indicates such things as inf-inf, 0*inf, 0/0, and inf/inf. The second occurs apparently only with rem(x,0). I am not sure why this particular operation was selected for a different NaN, but if it still works that way, you could use this for your purposes. However, you do have the problem of recognizing the difference afterwards. At the moment I cannot think of a way to do it.
>
> Roger Stafford

One way is to use typecast. If nan1 is the 7FF8000000000000 pattern NaN and nan2 is the 7FF8012000000000 pattern NaN, then this can be used:

typecast(nan1,'uint64') == typecast(nan2,'uint64')

i.e., using typecast will allow you to compare two NaN's. If they have the same bit pattern then the equal test will be true, and if they have different bit patterns the equal test will be false.


James Tursa

Subject: NaN2: Another NaN, an NaN alternative, two types of NaN?

From: Jos

Date: 12 Mar, 2009 09:11:02

Message: 8 of 9

"David Painter" <david.ross.painter@gmail.com> wrote in message <gpaejq$heu$1@fred.mathworks.com>...
> Hello,
>
> I never thought I would find myself asking this, but is there another NaN? I have two sources of NaN values in my program, "incalculable" NaNs (e.g., the answer of NaN - x) and "outlier" NaNs. I want to be able to see them at a glance in an array.
>
> I thought about using infinity (inf) for the outlier NaNs, but there are no infmean and infstd functions to complement nanmean and nanstd. I really don't want to be creating code to avoid this problem. Is something like the following available?
>
> a = [1 2 3 NaN 3 4 5 NaN2];
>
> If not, I would appreciate any one-line solutions: e.g., if(any(inf), ignore it, and nanmean(whatever remains). Inelegant to say the least.
>
> All the best,
>
> D

Why not keep an additional vector with your data that stores whether a value is OK, not a number, or an outlier? You probably already have some code to identify outliers ...

data = [3 4 5 6 9 5 3 4 NaN]
datacode = [1 1 1 2 3 1 1 1 2]

% and use it like:
validdata = data(datacode==1)
nandata = data(datacode==2)
outlierdata = data(datacode==3)

hth
Jos

Subject: NaN2: Another NaN, an NaN alternative, two types of NaN?

From: David Painter

Date: 14 Mar, 2009 05:44:01

Message: 9 of 9

Hello,

Thanks for all replies. Both solutions are good.

David

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us