Path: news.mathworks.com!not-for-mail
From: "John D'Errico" <woodchips@rochester.rr.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Give me a good example for accumarray()
Date: Sat, 4 Jul 2009 09:55:02 +0000 (UTC)
Organization: John D'Errico (1-3LEW5R)
Lines: 42
Message-ID: <h2n8tm$46f$1@fred.mathworks.com>
References: <h2n5b9$lo7$1@fred.mathworks.com>
Reply-To: "John D'Errico" <woodchips@rochester.rr.com>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1246701302 4303 172.30.248.35 (4 Jul 2009 09:55:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sat, 4 Jul 2009 09:55:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 869215
Xref: news.mathworks.com comp.soft-sys.matlab:552759


"Husam Aldahiyat" <numandina@gmail.com> wrote in message <h2n5b9$lo7$1@fred.mathworks.com>...
> Hello,
> I am learning MATLAB and when I read about the accumarray() function it looks good but I didn't understand how the examples worked. 
> 
> Could someone give me a nice small example (problem) and how accumarray solves it?

Any time that you wish to count the number of times
an element appears in a vector, accumarray is the
solution. Best is if the elements are small positive
integers.

x = ceil(10*rand(1,20))
x =
 7  1  9  10  7  8  8  4  7  2  8  1  3  1  1  9  7  4  10  1

count = accumarray(x',1)
count =
     5
     1
     1
     2
     0
     0
     4
     3
     2
     2

If they are not small positive integers, then the third
argument of unique will provide the extra piece.

If you wish to see accumarray in action, it forms an
important part of my own consolidator utility on the
file exchange:

http://www.mathworks.com/matlabcentral/fileexchange/8354

Of course, accumarray has other uses, since it can do
more than just count a list of numbers. But, this is
the task I set to accumarray more often than any other.

John