For Programmers: Free Programming Magazines  


Home > Archive > Matlab > April 2005 > Summing data grouped by a value









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Summing data grouped by a value
OnlyMe

2005-04-22, 4:04 pm

Hi,

say I have this:

a=[1034,1097,5467,1034,5467,109098]'

b=[24,35,22,108,56,12]'

I want

S =
1034 132 (ie 24+108)
1097 35
5467 78 (ie22+56)
109098 12

Is there any straightforward vectorised way of doing it (as the vectors may
be 100s long)?

Thanks





StephenLL

2005-04-22, 9:00 pm

OnlyMe wrote:
>
>
> Hi,
>
> say I have this:
>
> a=[1034,1097,5467,1034,5467,109098]'
>
> b=[24,35,22,108,56,12]'
>
> I want
>
> S =
> 1034 132 (ie 24+108)
> 1097 35
> 5467 78 (ie22+56)
> 109098 12
>
> Is there any straightforward vectorised way of doing it (as the
> vectors may
> be 100s long)?
>
> Thanks
>
>
>
>
>
>


Check out 'grpstats' in the stats toolbox and 'accumarray' in base
matlab.

Stephen
OnlyMe

2005-04-22, 9:00 pm


"StephenLL" <NOstephen.lienhardSPAM@ge.com> wrote in message
news:ef03b10.0@webx.raydaftYaTP...
> OnlyMe wrote:
>
> Check out 'grpstats' in the stats toolbox and 'accumarray' in base
> matlab.


Thanks I can get the sum from grpstats, however I dont have accumarray is
it new with 7 I have 6?




StephenLL

2005-04-22, 9:00 pm

OnlyMe wrote:
>
>
>
> "StephenLL" <NOstephen.lienhardSPAM@ge.com> wrote in message
> news:ef03b10.0@webx.raydaftYaTP...
(as the[color=darkred]
> base
>
> Thanks I can get the sum from grpstats, however I dont have
> accumarray is
> it new with 7 I have 6?
>
>
>
>
>


I just found out about it myself. According to a friend of mine who
is still on R13, he does not have it.

Stephen

Stephen
Dan Hensley

2005-04-22, 9:00 pm

On Fri, 22 Apr 2005 14:35:39 -0400, StephenLL wrote:

> OnlyMe wrote:

What are the constraints on matrix a? Are they all positive integers? If
so, use the sparse trick.

S=sparse(a,1,b);
S=[find(S) full(S(find(S)))];

Dan









[color=darkred]
>
> Check out 'grpstats' in the stats toolbox and 'accumarray' in base
> matlab.
>
> Stephen


OnlyMe

2005-04-22, 9:00 pm


"Dan Hensley" <dan@somewhere.net> wrote in message
news:pan.2005.04.22.21.13.18.219504@somewhere.net...
> On Fri, 22 Apr 2005 14:35:39 -0400, StephenLL wrote:
>
>
> What are the constraints on matrix a? Are they all positive integers? If
> so, use the sparse trick.
>
> S=sparse(a,1,b);
> S=[find(S) full(S(find(S)))];
>
> Dan



Yup. Nice one!



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com