{"id":127,"date":"2008-02-20T13:54:06","date_gmt":"2008-02-20T18:54:06","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2008\/02\/20\/under-appreciated-accumarray\/"},"modified":"2018-01-08T15:45:59","modified_gmt":"2018-01-08T20:45:59","slug":"under-appreciated-accumarray","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2008\/02\/20\/under-appreciated-accumarray\/","title":{"rendered":"Under-appreciated accumarray"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>The MATLAB function <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/accumarray.html\"><tt>accumarray<\/tt><\/a> seems to be under-appreciated.  <tt>accumarray<\/tt> allows you to aggregate items in an array in the way that you specify.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Newsgroup Statistics<\/a><\/li>\r\n         <li><a href=\"#2\">Recent Questions<\/a><\/li>\r\n         <li><a href=\"#4\">Next We Accumulate<\/a><\/li>\r\n         <li><a href=\"#5\">Another Way to Accumulate<\/a><\/li>\r\n         <li><a href=\"#7\">Other Accumulation Functions<\/a><\/li>\r\n         <li><a href=\"#11\">Derivative Work<\/a><\/li>\r\n         <li><a href=\"#12\">Do You accum?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Newsgroup Statistics<a name=\"1\"><\/a><\/h3>\r\n   <p>Since <tt>accumarray<\/tt> has been in MATLAB (7.0, R14), there have been over 100 threads in the <a>MATLAB newsgroup<\/a> where <tt>accumarray<\/tt> arose as a solution.\r\n   <\/p>\r\n   <h3>Recent Questions<a name=\"2\"><\/a><\/h3>\r\n   <p>One of the <a>more recent threads<\/a> asks how to aggregate values in one list based on another list.  Suppose the lists are\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">group = [1 2 2 2 3 3]'\r\ndata = [6 43 3 4 2 5]'<\/pre><pre style=\"font-style:oblique\">group =\r\n     1\r\n     2\r\n     2\r\n     2\r\n     3\r\n     3\r\ndata =\r\n     6\r\n    43\r\n     3\r\n     4\r\n     2\r\n     5\r\n<\/pre><p>and the goal is to <tt>sum<\/tt> the <tt>data<\/tt> in each <tt>group<\/tt>.  Let's first create the first input argument.  <tt>accumarray<\/tt> wants the an array of subscripts of the <tt>data<\/tt> pertaining to which output value the <tt>data<\/tt> belongs to. Since we're just producing a column vector with 3 values, we just append a column of <tt>ones<\/tt> to the <tt>group<\/tt> vector.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">indices = [group ones(size(group))]<\/pre><pre style=\"font-style:oblique\">indices =\r\n     1     1\r\n     2     1\r\n     2     1\r\n     2     1\r\n     3     1\r\n     3     1\r\n<\/pre><h3>Next We Accumulate<a name=\"4\"><\/a><\/h3>\r\n   <p>Since the default function for accumulation is <tt>sum<\/tt>, we can use the simplest form of <tt>accumarray<\/tt> to get the desired results.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">sums = accumarray(indices, data)<\/pre><pre style=\"font-style:oblique\">sums =\r\n     6\r\n    50\r\n     7\r\n<\/pre><h3>Another Way to Accumulate<a name=\"5\"><\/a><\/h3>\r\n   <p>We can instead accumulate the results by adding 2 input arguments to the function call.  These are the a size vector for the\r\n      output array and a function handle specifying the accumulating function.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">sums1 = accumarray(indices, data, [numel(unique(group)) 1], @sum)<\/pre><pre style=\"font-style:oblique\">sums1 =\r\n     6\r\n    50\r\n     7\r\n<\/pre><p>It's easy to see that the results from the two function calls are the same.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">isequal(sums, sums1)<\/pre><pre style=\"font-style:oblique\">ans =\r\n     1\r\n<\/pre><h3>Other Accumulation Functions<a name=\"7\"><\/a><\/h3>\r\n   <p>Sometimes, summing the results isn't what I'm looking for.  Having puzzled out the 4 input call syntax, I can now simply replace\r\n      the accumulation function.  To find the maximum values in each <tt>group<\/tt>, I use this code.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">maxData = accumarray(indices, data, [numel(unique(group)) 1], @max)<\/pre><pre style=\"font-style:oblique\">maxData =\r\n     6\r\n    43\r\n     5\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">maxData = accumarray(indices, data, [numel(unique(group)) 1], <span style=\"color: #0000FF\">...<\/span>\r\n    @(x)~any(isfinite(x)))<\/pre><pre style=\"font-style:oblique\">maxData =\r\n     0\r\n     0\r\n     0\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">data(end) = Inf\r\nmaxData = accumarray(indices, data, [numel(unique(group)) 1], <span style=\"color: #0000FF\">...<\/span>\r\n    @(x)~any(isfinite(x)))<\/pre><pre style=\"font-style:oblique\">data =\r\n     6\r\n    43\r\n     3\r\n     4\r\n     2\r\n   Inf\r\nmaxData =\r\n     0\r\n     0\r\n     0\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">maxData = accumarray(indices, data, [numel(unique(group)) 1], <span style=\"color: #0000FF\">...<\/span>\r\n    @(x)all(isfinite(x)))<\/pre><pre style=\"font-style:oblique\">maxData =\r\n     1\r\n     1\r\n     0\r\n<\/pre><h3>Derivative Work<a name=\"11\"><\/a><\/h3>\r\n   <p>John D'Errico made a more general function <a title=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadFile.do?objectId=8354&amp;objectType=file (link no longer works)\"><tt>consolidator<\/tt><\/a>, found on the MathWorks File Exchange to allow you to do some extra aggregation.  For example, <tt>consolidator<\/tt> allows the aggregation of elements when they are within a specified tolerance and not just identical.\r\n   <\/p>\r\n   <h3>Do You accum?<a name=\"12\"><\/a><\/h3>\r\n   <p>Some other obvious accumulation functions you might use include <tt>sum<\/tt>, <tt>max<\/tt>, <tt>min<\/tt>, <tt>prod<\/tt>. What functions do you use in situations when you aggregate with <tt>accumarray<\/tt>? \r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_139129616db84177a5f259f31dd93678() {\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='139129616db84177a5f259f31dd93678 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 139129616db84177a5f259f31dd93678';\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 2008 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_139129616db84177a5f259f31dd93678()\"><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.5<br><\/p>\r\n<\/div>\r\n<!--\r\n139129616db84177a5f259f31dd93678 ##### SOURCE BEGIN #####\r\n%% Under-appreciated accumarray\r\n% The MATLAB function\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/accumarray.html |accumarray|> \r\n% seems to be under-appreciated.  |accumarray| allows you to aggregate\r\n% items in an array in the way that you specify.  \r\n%% Newsgroup Statistics\r\n% Since |accumarray| has been in MATLAB (7.0, R14), there have been over\r\n% 100 threads in the \r\n% <http:\/\/search_results?query=accumarray&search=+Go+&sort=date_up&search_string=accumarray&page=1&search_submit=cssm&dur=all MATLAB newsgroup>\r\n% where |accumarray| arose as a solution.  \r\n%% Recent Questions\r\n% One of the\r\n% <http:\/\/view_thread\/164109#416121 more recent threads>\r\n% asks how to aggregate values in one list based on another list.  Suppose\r\n% the lists are \r\ngroup = [1 2 2 2 3 3]'\r\ndata = [6 43 3 4 2 5]'\r\n%%\r\n% and the goal is to |sum| the |data| in each |group|.  Let's first create\r\n% the first input argument.  |accumarray| wants the an array of subscripts\r\n% of the |data| pertaining to which output value the |data| belongs to.\r\n% Since we're just producing a column vector with 3 values, we just append\r\n% a column of |ones| to the |group| vector.\r\nindices = [group ones(size(group))]\r\n%% Next We Accumulate\r\n% Since the default function for accumulation is |sum|, we can use the\r\n% simplest form of |accumarray| to get the desired results.\r\nsums = accumarray(indices, data)\r\n%% Another Way to Accumulate\r\n% We can instead accumulate the results by adding 2 input arguments to the\r\n% function call.  These are the a size vector for the output array and a\r\n% function handle specifying the accumulating function.\r\nsums1 = accumarray(indices, data, [numel(unique(group)) 1], @sum)\r\n%%\r\n% It's easy to see that the results from the two function calls are the\r\n% same.\r\nisequal(sums, sums1)\r\n%% Other Accumulation Functions\r\n% Sometimes, summing the results isn't what I'm looking for.  Having\r\n% puzzled out the 4 input call syntax, I can now simply replace the\r\n% accumulation function.  To find the maximum values in each |group|, I use\r\n% this code.\r\nmaxData = accumarray(indices, data, [numel(unique(group)) 1], @max)\r\n%%\r\nmaxData = accumarray(indices, data, [numel(unique(group)) 1], ...\r\n    @(x)~any(isfinite(x)))\r\n%%\r\ndata(end) = Inf\r\nmaxData = accumarray(indices, data, [numel(unique(group)) 1], ...\r\n    @(x)~any(isfinite(x)))\r\n%%\r\nmaxData = accumarray(indices, data, [numel(unique(group)) 1], ...\r\n    @(x)all(isfinite(x)))\r\n%% Derivative Work\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadAuthor.do?objectId=801347&objectType=author John D'Errico>\r\n% made a more general function \r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadFile.do?objectId=8354&objectType=file |consolidator|>,\r\n% found on the MathWorks File Exchange to allow you to do some extra\r\n% aggregation.  For example, |consolidator| allows the aggregation of\r\n% elements when they are within a specified tolerance and not just\r\n% identical.\r\n%% Do You accum?\r\n% Some other obvious accumulation functions you might use include |sum|,\r\n% |max|, |min|, |prod|. What functions do you use in situations when you\r\n% aggregate with |accumarray|?  Let me know\r\n% <https:\/\/blogs.mathworks.com\/?p=127#respond here>.\r\n \r\n\r\n##### SOURCE END ##### 139129616db84177a5f259f31dd93678\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      The MATLAB function accumarray seems to be under-appreciated.  accumarray allows you to aggregate items in an array in the way that you specify.\r\n      \r\n   \r\n   Contents\r\n   \r\n ... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2008\/02\/20\/under-appreciated-accumarray\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[15,6],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/127"}],"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=127"}],"version-history":[{"count":4,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/127\/revisions"}],"predecessor-version":[{"id":2600,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/127\/revisions\/2600"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}