Doug’s MATLAB Video Tutorials

October 2nd, 2009

Basics: Using ACCUMARRAY

ACCUMARRAY is one of my favorite functions that I never seem to need outside of MATLAB golf problems. I present this to you with the hopes that knowing it exists allows you to use it at just the right time. The video discusses the following code:
sales.rep    = [1 2 1 3 4 6 2 3 1 1];
sales.amount = [1 1 3 1 5 2 2 4 1 5];


numReps = max(sales.rep);
sales.total   = zeros(1,numReps);
for i = 1 : numel(sales.rep)
    sales.total(sales.rep(i)) = sales.total(sales.rep(i)) ...
                              + sales.amount(i);
end


sales.accum = accumarray(sales.rep', sales.amount)'

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Doug Hull is a proud MathWorker who is on a mission to help you with MATLAB.

Doug's picture

These postings are the author's and don't necessarily represent the opinions of The MathWorks.