http://www.mathworks.com/matlabcentral/newsreader/view_thread/246472
MATLAB Central Newsreader  NaN2: Another NaN, an NaN alternative, two types of NaN?
Feed for thread: NaN2: Another NaN, an NaN alternative, two types of NaN?
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Thu, 12 Mar 2009 07:46:03 +0000
NaN2: Another NaN, an NaN alternative, two types of NaN?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/246472#634248
David Painter
Hello,<br>
<br>
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.<br>
<br>
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?<br>
<br>
a = [1 2 3 NaN 3 4 5 NaN2];<br>
<br>
If not, I would appreciate any oneline solutions: e.g., if(any(inf), ignore it, and nanmean(whatever remains). Inelegant to say the least.<br>
<br>
All the best,<br>
<br>
D

Thu, 12 Mar 2009 08:23:52 +0000
Re: NaN2: Another NaN, an NaN alternative, two types of NaN?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/246472#634253
Roger Stafford
"David Painter" <david.ross.painter@gmail.com> wrote in message <gpaejq$heu$1@fred.mathworks.com>...<br>
> Hello,<br>
> <br>
> 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.<br>
> <br>
> 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?<br>
> <br>
> a = [1 2 3 NaN 3 4 5 NaN2];<br>
> <br>
> If not, I would appreciate any oneline solutions: e.g., if(any(inf), ignore it, and nanmean(whatever remains). Inelegant to say the least.<br>
> <br>
> All the best,<br>
> <br>
> D<br>
<br>
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 nonzero bit is considered a NaN in the standard.<br>
<br>
In matlab I have found only two different NaNs on my ancient system, 7FF8000000000000 and 7FF8012000000000. The first of these indicates such things as infinf, 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.<br>
<br>
Roger Stafford

Thu, 12 Mar 2009 08:26:02 +0000
Re: NaN2: Another NaN, an NaN alternative, two types of NaN?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/246472#634257
Sebastian H?lz
Just generate a second logical index containing your outliers:<br>
<br>
a = [1 2 3 nan 5 6 n 1000 nan 10]; % here the nan's are the "uncalculatables"<br>
uncalcs = isnan(a); % This is the index of "uncalculatables"<br>
outliers = a<0 & a>100; % ... and the index of outliers<br>
a(outliers)=nan; % Set outliers to nan<br>
<br>
You can now do your "nanmean" and "nanstd", which are by the way not part of the Matlabstandard (at least not in Matlab 7.5), and will still know the caracteristics of your data. Alternatively to these nonstandardfunctions, you can quickly calculate mean and standard with:<br>
<br>
mean_a = mean(a(~isnan(a)));<br>
std_a = std(a(~isnan(a)));<br>
<br>
Sebastian

Thu, 12 Mar 2009 08:29:01 +0000
Re: NaN2: Another NaN, an NaN alternative, two types of NaN?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/246472#634258
Roger Stafford
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gpagqo$b9e$1@fred.mathworks.com>...<br>
> ......<br>
> ...... However, you do have the problem of recognizing the difference afterwards. At the moment I cannot think of a way to do it.<br>
> ......<br>
<br>
... except of course by using 'format hex'.<br>
<br>
Roger Stafford

Thu, 12 Mar 2009 09:04:04 +0000
Re: NaN2: Another NaN, an NaN alternative, two types of NaN?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/246472#634265
Bruno Luong
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gpah4d$183$1@fred.mathworks.com>...<br>
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gpagqo$b9e$1@fred.mathworks.com>...<br>
> > ......<br>
> > ...... However, you do have the problem of recognizing the difference afterwards. At the moment I cannot think of a way to do it.<br>
> > ......<br>
> <br>
> ... except of course by using 'format hex'.<br>
> <br>
> Roger Stafford<br>
<br>
Practical implementation of Roger's remark<br>
<br>
% function to generate an NaN of specific type 1, 2, ..., 255<br>
nant = @(type) typecast(uint8([type 0 0 0 0 0 248 255]),'double');<br>
<br>
% Function which get the type, 0 if Matlab double or NaN<br>
gettype = @(x) isnan(x).*double(subsref(typecast(x,'uint8'), ...<br>
substruct('()',{1})));<br>
<br>
x1 = nant(1)<br>
x2 = nant(2)<br>
<br>
gettype(x1)<br>
gettype(x2)<br>
<br>
Bruno

Thu, 12 Mar 2009 09:04:04 +0000
Re: NaN2: Another NaN, an NaN alternative, two types of NaN?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/246472#634266
James Tursa
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gpah4d$183$1@fred.mathworks.com>...<br>
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gpagqo$b9e$1@fred.mathworks.com>...<br>
> > ......<br>
> > ...... However, you do have the problem of recognizing the difference afterwards. At the moment I cannot think of a way to do it.<br>
> > ......<br>
> <br>
> ... except of course by using 'format hex'.<br>
> <br>
> Roger Stafford<br>
<br>
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.<br>
<br>
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:<br>
<br>
#include "mex.h"<br>
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])<br>
{<br>
unsigned long *ulp;<br>
plhs[0] = mxCreateDoubleScalar(0.0);<br>
ulp = mxGetData(plhs[0]);<br>
*ulp++ = 0xFFFFFFFFu;<br>
*ulp = 0xFFFFFFFFu;<br>
}<br>
<br>
When you look at the results of this mex function with format hex, you get:<br>
<br>
ans =<br>
ffffffffffffffff<br>
<br>
But when you do a num2hex on this same value, you get:<br>
<br>
ans =<br>
fff8000000000000<br>
<br>
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:<br>
<br>
>> 0<br>
ans =<br>
8000000000000000<br>
>> num2hex(0)<br>
ans =<br>
0000000000000000<br>
<br>
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.<br>
<br>
James Tursa

Thu, 12 Mar 2009 09:08:01 +0000
Re: NaN2: Another NaN, an NaN alternative, two types of NaN?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/246472#634271
James Tursa
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gpagqo$b9e$1@fred.mathworks.com>...<br>
> <br>
> In matlab I have found only two different NaNs on my ancient system, 7FF8000000000000 and 7FF8012000000000. The first of these indicates such things as infinf, 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.<br>
> <br>
> Roger Stafford<br>
<br>
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:<br>
<br>
typecast(nan1,'uint64') == typecast(nan2,'uint64')<br>
<br>
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.<br>
<br>
<br>
James Tursa

Thu, 12 Mar 2009 09:11:02 +0000
Re: NaN2: Another NaN, an NaN alternative, two types of NaN?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/246472#634272
Jos
"David Painter" <david.ross.painter@gmail.com> wrote in message <gpaejq$heu$1@fred.mathworks.com>...<br>
> Hello,<br>
> <br>
> 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.<br>
> <br>
> 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?<br>
> <br>
> a = [1 2 3 NaN 3 4 5 NaN2];<br>
> <br>
> If not, I would appreciate any oneline solutions: e.g., if(any(inf), ignore it, and nanmean(whatever remains). Inelegant to say the least.<br>
> <br>
> All the best,<br>
> <br>
> D<br>
<br>
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 ...<br>
<br>
data = [3 4 5 6 9 5 3 4 NaN]<br>
datacode = [1 1 1 2 3 1 1 1 2]<br>
<br>
% and use it like:<br>
validdata = data(datacode==1)<br>
nandata = data(datacode==2) <br>
outlierdata = data(datacode==3)<br>
<br>
hth<br>
Jos

Sat, 14 Mar 2009 05:44:01 +0000
Re: NaN2: Another NaN, an NaN alternative, two types of NaN?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/246472#634798
David Painter
Hello,<br>
<br>
Thanks for all replies. Both solutions are good.<br>
<br>
David