Path: news.mathworks.com!newsfeed-00.mathworks.com!nlpi057.nbdc.sbc.com!prodigy.net!news.glorb.com!news2.glorb.com!tr22g12.aset.psu.edu!news.mathforum.org!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: HOWTO: Accelerate processing algorithm
Date: Sun, 05 Jul 2009 12:17:45 EDT
Organization: The Math Forum
Lines: 41
Message-ID: <19990901.76079.1246810695595.JavaMail.jakarta@nitrogen.mathforum.org>
References: <1e37765c-af3c-49d8-8465-8203f0c78f19@d4g2000yqa.googlegroups.com>
NNTP-Posting-Host: nitrogen.mathforum.org
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: support1.mathforum.org 1246810695 4789 144.118.30.135 (5 Jul 2009 16:18:15 GMT)
X-Complaints-To: news@news.mathforum.org
NNTP-Posting-Date: Sun, 5 Jul 2009 16:18:15 +0000 (UTC)
Xref: news.mathworks.com comp.soft-sys.matlab:552900


Hi!

This
S = accumarray(yp1, v(:))./accumarray(yp1,1);

Is equivalent to
S = accumarray(yp1, v(:), [], @mean)

Regarding the documentation, I have to admit it is also quite weird to me.

What I wanted to do was to obtain an array, S, whose indices are specified by yp1, formed as the average of the values in v which are equal to the indices.

For that purpose
accumarray([1 1 3 2 2 5].', [1 2 3 4 5 6].')
1 -> 1 + 2
2 -> 4 + 5
3 -> 3
4
5 -> 6

And if you specify @fun, it performs any operation with the grouped elements
accumarray([1 1 3 2 2 5].', [1 2 3 4 5 6].', [], @fun)
1 -> fun(1,2)
2 -> fun(4,5)
3 -> fun(3)
4
5 -> fun(6)

The final suggestion by Bruno basically performs the mean of the groups, because it gathers on one hand the elements of v and on the other all 1s, and sums the contents.
accumarray([1 1 3 2 2 5].', [1 2 3 4 5 6].')
accumarray([1 1 3 2 2 5].', [1 1 1 1 1 1].')
1 -> 1 + 2           1 + 1
2 -> 4 + 5           1 + 1
3 -> 3               1
4                    0
5 -> 6               1

It has worked fine for me, but explaining it to you now I have just realized that there might be a problem with 0/0.

Hope it helps!
Jose