{"id":3339,"date":"2019-06-17T09:18:16","date_gmt":"2019-06-17T14:18:16","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=3339"},"modified":"2019-07-09T08:45:53","modified_gmt":"2019-07-09T13:45:53","slug":"dimsum-in-matlab","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2019\/06\/17\/dimsum-in-matlab\/","title":{"rendered":"dimsum in MATLAB"},"content":{"rendered":"\r\n\r\n<div class=\"content\"><!--introduction--><p>As Steve noted in a <a href=\"https:\/\/blogs.mathworks.com\/steve\/2019\/04\/30\/changes-to-sum-and-dim\/\">recent post<\/a>, several of the functions that operate along specific dimensions have been updated to add some new functionality, for example the function <tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/sum.html\">sum<\/a><\/tt>.  (I also think he missed a great opportunity for a cool blog post title, which is only partly why I am writing about it now :^) ).  Please read his post!<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2019\/dimsum.jpg\" alt=\"\"> <\/p><!--\/introduction--><h3>Contents<\/h3><div><ul><li><a href=\"#114296e2-8eb7-4e35-82f1-c40bf52305e8\">Summing Elements of an Array<\/a><\/li><li><a href=\"#d5ce142e-4a05-4e44-97bc-ffcf9efd8e9f\">An Aside - the Engineering Rule of Thumb<\/a><\/li><li><a href=\"#20e4bab6-01e6-40c2-a9cf-374a457bc450\">Back to Dimensions<\/a><\/li><li><a href=\"#23d2d735-b62f-471c-a20f-76cc547c8afa\">Your Feedback<\/a><\/li><\/ul><\/div><h4>Summing Elements of an Array<a name=\"114296e2-8eb7-4e35-82f1-c40bf52305e8\"><\/a><\/h4><p>As you likely know, MATLAB has a collection of functions that operate on inputs, treating the effective column vectors as individual units.  There functions include <tt>sum, prod, cumsum, mean<\/tt>, to name a few.<\/p><p>Originally, you could only use these functions with a single input.  And the operation (+, *, etc.) would occur along the first non-singleton dimension.  This tripped people up enough and was inconvenient enough that we eventually added an optional <tt>dimension<\/tt> argument.  And, if you wanted to apply the operation to the entire array, we taught you to string the input into a long column vector (<tt>A(:)<\/tt>) and apply the necessary function to this transformed array.<\/p><h4>An Aside - the Engineering Rule of Thumb<a name=\"d5ce142e-4a05-4e44-97bc-ffcf9efd8e9f\"><\/a><\/h4><p>We had a wonderful engineer here many years ago that taught me the \"engineering rule of thumb\".  Either you disallow something always, you allow it once, or you always allow it.  This got shortened to the \"0, 1, or infinity rule\".  Look at the case of <tt>sum<\/tt> as a representative function. If you simply require the default behavior, you need not supply any additional arguments beyond the array itself (0), you can use an optional scalar argument (1) to specify the dimension to work on, and now you can have that optional argument be a vector of dimensions (infinity, theoretically  - but you'll never fit that into MATLAB!).<\/p><h4>Back to Dimensions<a name=\"20e4bab6-01e6-40c2-a9cf-374a457bc450\"><\/a><\/h4><p>In R2018b we added a perhaps friendlier syntax for the <tt>func(A(:))<\/tt> syntax, now <tt>func(A,'all')<\/tt>.  This is in addition to allowing no dimension input, a scalar dimension input, or a vector of dimensions - 0, 1, infinity.<\/p><p>Here's the current list of functions to which these new input patterns apply.<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2019\/dims.png\" alt=\"\"> <\/p><h4>Your Feedback<a name=\"23d2d735-b62f-471c-a20f-76cc547c8afa\"><\/a><\/h4><p>Is this new feature helpful to you? Let us know <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=3339#respond\">here<\/a>.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_73a365225f2149cbafbab8ec4e678f9d() {\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='73a365225f2149cbafbab8ec4e678f9d ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 73a365225f2149cbafbab8ec4e678f9d';\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        copyright = 'Copyright 2019 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 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');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\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_73a365225f2149cbafbab8ec4e678f9d()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2019a<br><\/p><\/div><!--\r\n73a365225f2149cbafbab8ec4e678f9d ##### SOURCE BEGIN #####\r\n%% dimsum in MATLAB\r\n% As Steve noted in a\r\n% <https:\/\/blogs.mathworks.com\/steve\/2019\/04\/30\/changes-to-sum-and-dim\/\r\n% recent post>, several of the functions that operate along specific\r\n% dimensions have been updated to add some new functionality, for\r\n% example the function |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/sum.html\r\n% sum>|.  (I also think he missed a great opportunity for a cool blog post\r\n% title, which is only partly why I am writing about it now :^) ).  Please\r\n% read his post!\r\n%\r\n% <<dimsum.jpg>>\r\n%\r\n%% Summing Elements of an Array\r\n% As you likely know, MATLAB has a collection of functions that operate on\r\n% inputs, treating the effective column vectors as individual units.  There\r\n% functions include |sum, prod, cumsum, mean|, to name a few.\r\n%%\r\n% Originally, you could only use these functions with a single input.  And\r\n% the operation (+, *, etc.) would occur along the first non-singleton\r\n% dimension.  This tripped people up enough and was inconvenient enough\r\n% that we eventually added an optional |dimension| argument.  And, if you\r\n% wanted to apply the operation to the entire array, we taught you to\r\n% string the input into a long column vector (|A(:)|) and apply the\r\n% necessary function to this transformed array.  \r\n%% An Aside - the Engineering Rule of Thumb\r\n% We had a wonderful engineer here many years ago that taught me the\r\n% \"engineering rule of thumb\".  Either you disallow something always, you\r\n% allow it once, or you always allow it.  This got shortened to the \"0, 1,\r\n% or infinity rule\".  Look at the case of |sum| as a representative\r\n% function. If you simply require the default behavior, you need not supply\r\n% any additional arguments beyond the array itself (0), you can use an\r\n% optional scalar argument (1) to specify the dimension to work on, and now\r\n% you can have that optional argument be a vector of dimensions (infinity,\r\n% theoretically  - but you'll never fit that into MATLAB!).  \r\n%% Back to Dimensions\r\n% In R2018b we added a perhaps friendlier syntax for the |func(A(:))|\r\n% syntax, now |func(A,'all')|.  This is in addition to allowing no\r\n% dimension input, a scalar dimension input, or a vector of dimensions - 0,\r\n% 1, infinity.\r\n%\r\n% Here's the current list of functions to which these new input patterns\r\n% apply.\r\n%\r\n% <<dims.png>>\r\n%% Your Feedback\r\n% Is this new feature helpful to you? Let us know\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=3339#respond here>.\r\n##### SOURCE END ##### 73a365225f2149cbafbab8ec4e678f9d\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2019\/dimsum.jpg\" onError=\"this.style.display ='none';\" \/><\/div><!--introduction--><p>As Steve noted in a <a href=\"https:\/\/blogs.mathworks.com\/steve\/2019\/04\/30\/changes-to-sum-and-dim\/\">recent post<\/a>, several of the functions that operate along specific dimensions have been updated to add some new functionality, for example the function <tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/sum.html\">sum<\/a><\/tt>.  (I also think he missed a great opportunity for a cool blog post title, which is only partly why I am writing about it now :^) ).  Please read his post!... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2019\/06\/17\/dimsum-in-matlab\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/3339"}],"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=3339"}],"version-history":[{"count":5,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/3339\/revisions"}],"predecessor-version":[{"id":3396,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/3339\/revisions\/3396"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=3339"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=3339"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=3339"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}