{"id":289,"date":"2011-09-23T12:22:00","date_gmt":"2011-09-23T12:22:00","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2011\/09\/23\/another-possible-surprise-ignored-nan-values\/"},"modified":"2011-09-19T12:26:08","modified_gmt":"2011-09-19T12:26:08","slug":"another-possible-surprise-ignored-nan-values","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2011\/09\/23\/another-possible-surprise-ignored-nan-values\/","title":{"rendered":"Another Possible Surprise &#8211; Ignored NaN Values"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>Ever have some data that <i>might<\/i> have some <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/nan.html\"><tt>NaN<\/tt><\/a> values?  And you start doing computations with these data, expecting |NaN|s to propagate...  Only to find later, that the\r\n         |NaN|s only went so far.  Here's what's going on, for better or worse.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">NaNs Play Different Roles<\/a><\/li>\r\n         <li><a href=\"#2\">Some MATLAB Functions Ignore NaN Values<\/a><\/li>\r\n         <li><a href=\"#4\">Why Ignore NaNs?<\/a><\/li>\r\n         <li><a href=\"#5\">How Do You Use NaNs?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>NaNs Play Different Roles<a name=\"1\"><\/a><\/h3>\r\n   <p>I know of 3 typical uses of <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/ref\/nan.html\"><tt>NaN<\/tt><\/a> values:\r\n   <\/p>\r\n   <div>\r\n      <ul>\r\n         <li>to represent missing data<\/li>\r\n         <li>result of computational ambiguity (e.g., <tt>0\/0<\/tt>)\r\n         <\/li>\r\n         <li>graphics directive to separate line segments in a single vector of point values<\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Some MATLAB Functions Ignore NaN Values<a name=\"2\"><\/a><\/h3>\r\n   <p>There are several MATLAB functions that ignore <tt>NaN<\/tt> valuess, including\r\n   <\/p>\r\n   <p><a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/ref\/min.html\"><tt>min<\/tt><\/a>, <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/ref\/max.html\"><tt>max<\/tt><\/a>, <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/ref\/any.html\"><tt>any<\/tt><\/a>, and <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/ref\/all.html\"><tt>all<\/tt><\/a> Here are a few little examples in action.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">any([0 nan])\r\nall([0 nan])\r\nall([1 nan])\r\nany([1 nan])<\/pre><pre style=\"font-style:oblique\">ans =\r\n     0\r\nans =\r\n     0\r\nans =\r\n     1\r\nans =\r\n     1\r\n<\/pre><p>There are also a bunch of <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011b\/toolbox\/stats\/bq_w_hm.html#brjjdzm-1\">functions in Statistics Toolbox<\/a> that ignore |NaN|s, including these:\r\n   <\/p>\r\n   <p><tt>nanmean, nanmin, nanmax, nancov, nanstd, nansum, nanvar, nanmedian<\/tt><\/p>\r\n   <h3>Why Ignore NaNs?<a name=\"4\"><\/a><\/h3>\r\n   <p>The reason <tt>min<\/tt>, <tt>max<\/tt>, <tt>any<\/tt> and <tt>all<\/tt> ignore <tt>NaN<\/tt> values is for the the graphics case - where line segments in a line are separated by <tt>NaN<\/tt>, and finding the <tt>min<\/tt> and <tt>max<\/tt> values is useful for setting axis limits.\r\n   <\/p>\r\n   <h3>How Do You Use NaNs?<a name=\"5\"><\/a><\/h3>\r\n   <p>Have you found the tools you need to work with your data containing <tt>NaN<\/tt> values? Do you need to work around them or do you embrace their presence? Let me know <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=289#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_1890ce2cab48480ba098742e9e807bf7() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='1890ce2cab48480ba098742e9e807bf7 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 1890ce2cab48480ba098742e9e807bf7';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Loren Shure';\r\n        copyright = 'Copyright 2011 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_1890ce2cab48480ba098742e9e807bf7()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.13<br><\/p>\r\n<\/div>\r\n<!--\r\n1890ce2cab48480ba098742e9e807bf7 ##### SOURCE BEGIN #####\r\n%% Another Possible Surprise - Ignored NaN Values\r\n% Ever have some data that _might_ have some\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/nan.html\r\n% |NaN|> values?  And you start doing computations with these data,\r\n% expecting |NaN|s to propagate...  Only to find later, that the |NaN|s\r\n% only went so far.  Here's what's going on, for better or worse.\r\n%% NaNs Play Different Roles\r\n% I know of 3 typical uses of\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/ref\/nan.html\r\n% |NaN|> values:\r\n%\r\n% * to represent missing data\r\n% * result of computational ambiguity (e.g., |0\/0|)\r\n% * graphics directive to separate line segments in a single vector of\r\n% point values\r\n%\r\n%% Some MATLAB Functions Ignore NaN Values\r\n% There are several MATLAB functions that ignore |NaN| valuess, including\r\n%\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/ref\/min.html\r\n% |min|>,\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/ref\/max.html\r\n% |max|>,\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/ref\/any.html\r\n% |any|>, and\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/ref\/all.html\r\n% |all|> Here are a few little examples in action.\r\nany([0 nan])\r\nall([0 nan])\r\nall([1 nan])\r\nany([1 nan])\r\n%%\r\n% There are also a bunch of\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011b\/toolbox\/stats\/bq_w_hm.html#brjjdzm-1\r\n% functions in Statistics Toolbox> that ignore |NaN|s, including these:\r\n%\r\n% |nanmean, nanmin, nanmax, nancov, nanstd, nansum, nanvar, nanmedian|\r\n%% Why Ignore NaNs?\r\n% The reason |min|, |max|, |any| and |all| ignore |NaN| values is for the\r\n% the graphics case - where line segments in a line are separated by\r\n% |NaN|, and finding the |min| and |max| values is useful for setting axis\r\n% limits.\r\n%% How Do You Use NaNs?\r\n% Have you found the tools you need to work with your data containing\r\n% |NaN| values? Do you need to work around them or do you embrace their presence? Let me know <https:\/\/blogs.mathworks.com\/loren\/?p=289#respond\r\n% here>.\r\n\r\n\r\n##### SOURCE END ##### 1890ce2cab48480ba098742e9e807bf7\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      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\r\n         |NaN|s... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2011\/09\/23\/another-possible-surprise-ignored-nan-values\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[14,21,11],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/289"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=289"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/289\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=289"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}