Path: news.mathworks.com!newsfeed-00.mathworks.com!panix!bloom-beacon.mit.edu!llnews!53ab2750!not-for-mail
From: Peter Boettcher <boettcher@ll.mit.edu>
Newsgroups: comp.soft-sys.matlab
Subject: Re: combine data?
References: <g5k9qk$kc5$1@fred.mathworks.com>
Message-ID: <muyhcaqrmuc.fsf@G99-Boettcher.llan.ll.mit.edu>
Organization: MIT Lincoln Laboratory
User-Agent: Gnus/5.110006 (No Gnus v0.6) Emacs/23.0.0 (gnu/linux)
Cancel-Lock: sha1:NdpWXIu2Rwvtf+U7QAjGDWwheUc=
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Lines: 29
Date: Wed, 16 Jul 2008 09:16:11 -0400
NNTP-Posting-Host: 155.34.163.114
X-Complaints-To: news@ll.mit.edu
X-Trace: llnews 1216213436 155.34.163.114 (Wed, 16 Jul 2008 09:03:56 EDT)
NNTP-Posting-Date: Wed, 16 Jul 2008 09:03:56 EDT
Xref: news.mathworks.com comp.soft-sys.matlab:479737



"jay vaughan" <jvaughan5.nospam@gmail.com> writes:

> Hi,
>
> I am trying to find a way to combine data without using a 
> loop. My data are similar to the following.
>
> width  = [1 4 4 5 6 10 10 10 16];
> weight = [1 1 2 1 1 4  2  2  1];
>
> I would like to find a way to combine all entries where the 
> width was the same, finding the total weight, like below.
>
> combined_width  = [1 4 5 6 10 16];
> combined_weight = [1 3 1 1 8  1];
>
> I have to do this a million times or so and was hoping to 
> do it efficiently. Any ideas on how to vectorize something 
> like this?

We used to do this with the "sparse" function, which had the neat side
effect of summing any values that were specified with repeat indices.
Now there is a dedicated function for it: accumarray.

[combined_width, a, b] = unique(width);
combined_weight=accumarray(b', weight).';

-Peter