Path: news.mathworks.com!newsfeed-00.mathworks.com!nlpi057.nbdc.sbc.com!prodigy.net!nx01.iad01.newshosting.com!newshosting.com!69.16.185.11.MISMATCH!npeer01.iad!news.highwinds-media.com!feed-me.highwinds-media.com!postnews.google.com!d77g2000hsb.googlegroups.com!not-for-mail
From: Rune Allnor <allnor@tele.ntnu.no>
Newsgroups: comp.soft-sys.matlab
Subject: Re: combine data?
Date: Wed, 16 Jul 2008 03:37:34 -0700 (PDT)
Organization: http://groups.google.com
Lines: 47
Message-ID: <57f37a55-f46c-4456-b93a-23b4a1f981a6@d77g2000hsb.googlegroups.com>
References: <g5k9qk$kc5$1@fred.mathworks.com>
NNTP-Posting-Host: 212.17.141.54
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
X-Trace: posting.google.com 1216204654 2247 127.0.0.1 (16 Jul 2008 10:37:34 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Wed, 16 Jul 2008 10:37:34 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: d77g2000hsb.googlegroups.com; posting-host=212.17.141.54; 
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET 
Xref: news.mathworks.com comp.soft-sys.matlab:479704



On 16 Jul, 10:00, "jay vaughan" <jvaughan5.nos...@gmail.com> wrote:
> Hi,
>
> I am trying to find a way to combine data without using a
> loop.

Sigh... the vixen of vectorisation in all her splendour...

It can't be done. There is no way to do this without a loop.

> My data are similar to the following.
>
> width =A0=3D [1 4 4 5 6 10 10 10 16];
> weight =3D [1 1 2 1 1 4 =A02 =A02 =A01];
>
> 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 =A0=3D [1 4 5 6 10 16];
> combined_weight =3D [1 3 1 1 8 =A01];
>
> I have to do this a million times or so and was hoping to
> do it efficiently.

That's a completely different issue. What I would do
if I was constrained to matlab (not tested!):

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wiv =3D unique(width);
wev =3D zeros(size(wiv));

for n=3D1:length(wev)
   idx=3Dfind(width=3D=3Dwiv(n));
   wev(n)=3Dsum(weight(idx));
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

If this is too slow, first learn a proper programming
language and then read

Knuth: "The Art of ComputerProgramming, vol 3:
       Sorting and Searching."

In fact, that's not such a bad idea even if the script
above runs fast enough for you.

Rune