Stuart’s MATLAB Videos

Watch and Learn

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)'
|

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.