Path: news.mathworks.com!newsfeed-00.mathworks.com!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!s13g2000prd.googlegroups.com!not-for-mail
From: Ross <fed.rossi@gmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: can I avoid a loop here?
Date: Tue, 19 Feb 2008 10:22:19 -0800 (PST)
Organization: http://groups.google.com
Lines: 42
Message-ID: <03f72c09-f9b8-4333-9eef-17e874374261@s13g2000prd.googlegroups.com>
References: <e01a38fc-431a-415f-9162-8e7ad7a422c0@q33g2000hsh.googlegroups.com> 
NNTP-Posting-Host: 165.124.164.96
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
X-Trace: posting.google.com 1203445339 32315 127.0.0.1 (19 Feb 2008 18:22:19 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Tue, 19 Feb 2008 18:22:19 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: s13g2000prd.googlegroups.com; posting-host=165.124.164.96; 
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.4) 
Xref: news.mathworks.com comp.soft-sys.matlab:452456



On Feb 19, 12:10 pm, "Steven Lord" <sl...@mathworks.com> wrote:
> "Ross" <fed.ro...@gmail.com> wrote in message
>
> news:e01a38fc-431a-415f-9162-8e7ad7a422c0@q33g2000hsh.googlegroups.com...
>
>
>
> > Hi,
> > I face the following problem:
>
> > Let j and x be two vectors of same length.
> > I would like to apply function FUN to the subset of xs, where the
> > subset is defined by the index j.
>
> > --------------
> > Eg:
>
> > j = [1 2 2 2 3 3]'
> > x = [6 43 3 4 2 5]'
>
> > FUN is sum
>
> > the result wanted is [6 50 7]'
> > -----------------
>
> > Is there a way to avoid looping?
> >> j = [1; 2; 2; 2; 3; 3];
> >> x = [6; 43; 3; 4; 2; 5];
> >> M = accumarray([j ones(size(j))], x)
>
> M =
>
>      6
>     50
>      7
>
> --
> Steve Lord
> sl...@mathworks.com

beautiful.
thanks Steve!