{"id":8824,"date":"2017-08-25T09:00:01","date_gmt":"2017-08-25T13:00:01","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=8824"},"modified":"2017-12-20T12:46:41","modified_gmt":"2017-12-20T17:46:41","slug":"summing-and-multiplying-nans-use-cases","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2017\/08\/25\/summing-and-multiplying-nans-use-cases\/","title":{"rendered":"Summing and Multiplying NaNs: Use cases???"},"content":{"rendered":"<div class=\"content\"><!--introduction--><!--\/introduction--><\/p>\n<h3>Contents<\/h3>\n<div>\n<ul>\n<li><a href=\"#35b69a93-c1f8-4445-b9b1-ce45070876a7\"><tt>nancumsum()<\/tt> , <tt>nancumprod()<\/tt><\/a><\/li>\n<li><a href=\"#8be0205c-f3b3-44a6-9345-b459e3dbf13b\">My Questions<\/a><\/li>\n<li><a href=\"#449f9ed7-5d36-4332-80da-6e0ba0a1a9a3\">For your consideration<\/a><\/li>\n<\/ul>\n<\/div>\n<h4><tt>nancumsum()<\/tt> , <tt>nancumprod()<\/tt><a name=\"35b69a93-c1f8-4445-b9b1-ce45070876a7\"><\/a><\/h4>\n<p>This week, instead of Picking a single file for highlighting, I (<a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/845693\">Brett<\/a>) would like to pose a couple of questions.<\/p>\n<p>Many years ago, in 2007 shortly after I started working for MathWorks, I needed at one point to calculate the cumulative sum of a vector, <i>ignoring NaNs<\/i>. Quite honestly, I don&#8217;t even remember why I needed to do so; perhaps it was to help a customer.<\/p>\n<p>Searching the MATLAB documentation, I quickly found my way to <a href=\"https:\/\/www.mathworks.com\/help\/stats\/nansum.html\"><tt>nansum<\/tt><\/a> in the <a href=\"https:\/\/www.mathworks.com\/products\/statistics.html\">Statistics and Machine Learning Toolbox<\/a>. (This, of course, was before &#8220;Machine Learning&#8221; was included in the name of that tool!) But that function didn&#8217;t do what I needed.<\/p>\n<p>Undaunted, I wrote my own version of <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/14895-nancumsum\"><tt>nancumsum<\/tt><\/a> and promptly shared it on the File Exchange. Following a flurry of emails addressing this contribution, I modified the function to provide additional modes of operation. In its current form, there are four modes:<\/p>\n<div>\n<ol>\n<li>REPLACE NaNs with zeros; (this is the default behavior).<\/li>\n<li>MAINTAIN NaNs as position holders; (that is, skip NaNs without reset).<\/li>\n<li>RESET sum on NaNs, replacing NaNs with zeros.<\/li>\n<li>RESET sum on NaNs, maintaining NaNs as position holders.<\/li>\n<\/ol>\n<\/div>\n<p>Just because it was easy to do, I also shared <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/14927-nancumprod\"><tt>nancumprod<\/tt><\/a>, which multiplies cumulatively in a similar vein.<\/p>\n<p>In the ten years since I shared those files, they have been downloaded thousands of times. (<tt>nancumsum<\/tt> is clearly way ahead of <tt>nancumprod<\/tt> in the number of downloads.) And the reviews have been quite good. (MATLABber extraordinaire Urs Schwarz pronounced it &#8220;a trouvaille of paramount importance to this community,&#8221; and suggested that it &#8220;should have been in [MATLAB] stock for a long time.&#8221; Thanks for that, Urs! ;) ) The functions&#8211;especially <tt>nancumsum<\/tt>&#8211;remain popular to this day.<\/p>\n<h4>My Questions<a name=\"8be0205c-f3b3-44a6-9345-b459e3dbf13b\"><\/a><\/h4>\n<p>So now I&#8217;m wondering <i>why<\/i> they&#8217;ve been so popular for so long. Just <i>how<\/i> are people using <tt>nancumsum<\/tt> and <tt>nancumprod<\/tt>?<\/p>\n<p>And more importantly: are those who are still using those functions aware that the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/cumsum.html\"><tt>cumsum<\/tt><\/a> function now has a &#8220;nanflag&#8221; to specify that NaNs are to be ignored? (In fact, &#8216;omitnan&#8217; works as of R2016b in <tt>cumsum<\/tt>, <tt>cummin<\/tt>, and <tt>cummax<\/tt>; and as of R2017a in <tt>prod<\/tt> and <tt>cumprod<\/tt>.)<\/p>\n<h4>For your consideration<a name=\"449f9ed7-5d36-4332-80da-6e0ba0a1a9a3\"><\/a><\/h4>\n<pre class=\"language-matlab\">A = [3 5 NaN 9 0 NaN]\r\nA =\r\n  3     5   NaN     9     0   NaN\r\n<\/pre>\n<pre class=\"language-matlab\">B = nancumsum(A,2,1) <span class=\"comment\">% Work along the second (row) dimension, using mode 1<\/span>\r\nB =\r\n  3     8     8    17    17    17\r\n<\/pre>\n<pre class=\"language-matlab\">C = cumsum(A,<span class=\"string\">'omitnan'<\/span>)\r\nC =\r\n  3     8     8    17    17    17\r\n<\/pre>\n<p><i>We see that the default mode of <tt>nancumsum<\/tt> is obviated by the new support for ignoring NaNs in <tt>cumsum<\/tt>!<\/i><\/p>\n<p>There are other modes of <tt>nancumsum<\/tt>, not supported by the MATLAB functions:<\/p>\n<pre>B2 = nancumsum(A,2,2)\r\nB2 =\r\n   3     8   NaN    17    17   NaN\r\nB3 = nancumsum(A,2,3)\r\nB3 =\r\n   3     8     0     9     9     0\r\nB4 = nancumsum(A,2,4)\r\nB4 =\r\n   3     8   NaN     9     9   NaN<\/pre>\n<p>Does anyone have any thoughts to share on the subject? Should I leave the files on the Exchange, or should I remove them to promote usage of the fully supported MATLAB functions? Do the alternate modes justify keeping the files alive?<\/p>\n<p>Your <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=8824#respond\">thoughts and comments<\/a> on the topic are very welcome!<\/p>\n<p><script language=\"JavaScript\"> <!-- \n    function grabCode_0166941455784ff4937d7f7e76301404() {\n        \/\/ Remember the title so we can use it in the new page\n        title = document.title;\n\n        \/\/ Break up these strings so that their presence\n        \/\/ in the Javascript doesn't mess up the search for\n        \/\/ the MATLAB code.\n        t1='0166941455784ff4937d7f7e76301404 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 0166941455784ff4937d7f7e76301404';\n    \n        b=document.getElementsByTagName('body')[0];\n        i1=b.innerHTML.indexOf(t1)+t1.length;\n        i2=b.innerHTML.indexOf(t2);\n \n        code_string = b.innerHTML.substring(i1, i2);\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\n\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \n        \/\/ in the XML parser.\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\n        \/\/ doesn't go ahead and substitute the less-than character. \n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\n\n        copyright = 'Copyright 2017 The MathWorks, Inc.';\n\n        w = window.open();\n        d = w.document;\n        d.write('\n\n<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\n\n\\n');\n\n        d.title = title + ' (MATLAB code)';\n        d.close();\n    }   \n     --> <\/script><\/p>\n<p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><a href=\"javascript:grabCode_0166941455784ff4937d7f7e76301404()\"><span style=\"font-size: x-small;        font-style: italic;\">Get<br \/>\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><\/p>\n<p>      Published with MATLAB&reg; R2017a<\/p>\n<\/div>\n<p><!--\n0166941455784ff4937d7f7e76301404 ##### SOURCE BEGIN #####\n%% Summing and Multiplying NaNs: Use cases???\n%% |nancumsum()| , |nancumprod()|\n%\n% This week, instead of Picking a single file for highlighting, I\n% would like to pose a couple of questions.\n%\n% Many years ago, in 2007 shortly after I started working for\n% MathWorks, I needed at one point to calculate the cumulative sum of\n% a vector, _ignoring NaNs_. Quite honestly, I don't even remember why\n% I needed to do so; perhaps it was to help a customer.\n%\n% Searching the MATLAB documentation, I quickly found my way to\n% <https:\/\/www.mathworks.com\/help\/stats\/nansum.html |nansum|> \n% in the <https:\/\/www.mathworks.com\/products\/statistics.html Statistics and Machine Learning Toolbox>.\n% (This, of course, was before \"Machine Learning\" was included in the name of that\n% tool!) But that function didn't do what I needed.\n%\n% Undaunted, I wrote my own version of\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/14895-nancumsum |nancumsum|>\n% and promptly shared it on the File Exchange. Following a flurry of emails addressing this contribution, I \n% modified the function to provide additional modes of operation. In its current form, \n% there are four modes:\n%%\n% \n% # REPLACE NaNs with zeros; (this is the default behavior).\n% # MAINTAIN NaNs as position holders; (that is, skip NaNs without reset).\n% # RESET sum on NaNs, replacing NaNs with zeros.\n% # RESET sum on NaNs, maintaining NaNs as position holders.\n% \n%%\n% Just because it was easy to do, I also shared <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/14927-nancumprod |nancumprod|>, which\n% multiplies cumulatively in a similar vein.\n%%\n% In the ten years since I shared those files, they have been\n% downloaded thousands of times. (|nancumsum| is clearly way ahead of\n% |nancumprod| in the number of downloads.) And the reviews have been\n% quite good. (MATLABber extraordinaire Urs Schwarz pronounced it \"a\n% trouvaille of paramount importance to this community,\" and suggested\n% that it \"should have been in [MATLAB] stock for a long time.\" Thanks\n% for that, Urs! ;) ) The functionsREPLACE_WITH_DASH_DASHespecially |nancumsum|REPLACE_WITH_DASH_DASHremain\n% popular to this day.\n\n%% My Questions\n% So now I'm wondering _why_ they've been so popular for so long. Just _how_\n% are people using |nancumsum| and |nancumprod|? \n%%\n% And more importantly: are those who are still using those functions aware\n% that the <https:\/\/www.mathworks.com\/help\/matlab\/ref\/cumsum.html |cumsum|> \n% function now has a \"nanflag\" to specify that NaNs are to\n% be ignored? (In fact, 'omitnan' works as of R2016b in |cumsum|,\n% |cummin|, and |cummax|; and as of R2017a in |prod| and |cumprod|.)\n%% For your consideration\n%   A = [3 5 NaN 9 0 NaN]\n% A =\n%     3     5   NaN     9     0   NaN\n%%\n%   B = nancumsum(A,2,1) % Work along the second (row) dimension, using mode 1\n% B =\n%     3     8     8    17    17    17\n%%   \n%   C = cumsum(A,'omitnan')\n% C =\n%     3     8     8    17    17    17\n%%\n% _We see that the default mode of |nancumsum| is obviated by the new support for ignoring NaNs in |cumsum|!_\n%%\n% There are other modes of |nancumsum|, not supported by the MATLAB\n% functions:\n%%\n%  B2 = nancumsum(A,2,2)\n% B2 =\n%     3     8   NaN    17    17   NaN\n%  B3 = nancumsum(A,2,3)\n% B3 =\n%     3     8     0     9     9     0\n%  B4 = nancumsum(A,2,4)\n% B4 =\n%     3     8   NaN     9     9   NaN\n%%\n% Does anyone have any thoughts to share on the subject? Should I\n% leave the files on the Exchange, or should I remove them to promote\n% usage of the fully supported MATLAB functions? Do the alternate\n% modes justify keeping the files alive? \n%%\n% Your <https:\/\/blogs.mathworks.com\/pick\/?p=8824#respond thoughts and comments> on the topic are very welcome!\n##### SOURCE END ##### 0166941455784ff4937d7f7e76301404\n--><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\nContents<\/p>\n<p>nancumsum() , nancumprod()<br \/>\nMy Questions<br \/>\nFor your consideration<\/p>\n<p>nancumsum() , nancumprod()<br \/>\nThis week, instead of Picking a single file for highlighting, I (Brett) would like to pose a&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2017\/08\/25\/summing-and-multiplying-nans-use-cases\/\">read more >><\/a><\/p>\n","protected":false},"author":34,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/8824"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/34"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=8824"}],"version-history":[{"count":12,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/8824\/revisions"}],"predecessor-version":[{"id":9062,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/8824\/revisions\/9062"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=8824"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=8824"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=8824"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}