A more efficient version:

      Key←{⎕ML←3 ⋄ ⍺⍺¨i[j]⊂⍵[j←⍋i←1+⍺⍳⍺]}
      x←1 1 1 1 2 2 2 33 33 33 
      y←100 100 20 400 30 200 300 100 100 100
      x +/Key y
620 530 300



----- Original Message -----
From: Roger Hui <[email protected]>
Date: Tuesday, May 17, 2011 16:14
Subject: Re: [Jprogramming] Aggregation
To: Programming forum <[email protected]>

> It should not be verbose even in APL:
> 
>       Key←{⍺⍺¨(↓(∪⍺)∘.=⍺)/¨⊂⍵}
> 
>       x←1 1 1 1 2 2 2 33 33 33 
>       y←100 100 20 400 30 200 300 100 
> 100 100
>       x +/ Key y
> 620 530 300
> 
>       p←10?10
>       x[p]
> 1 33 33 33 2 2 1 2 1 1
>       y[p]
> 20 100 100 100 300 200 400 30 100 100
>       x[p] +/ Key y[p]
> 620 300 530
> 
> 
> 
> ----- Original Message -----
> From: Ian Clark <[email protected]>
> Date: Tuesday, May 17, 2011 15:28
> Subject: Re: [Jprogramming] Aggregation
> To: Programming forum <[email protected]>
> 
> > Thanks folx, that's pretty useful.
> > 
> > Our author should be pleased to know it. He's been doing it a
> > long-winded way in APL.
> > 
> > So... everyone on the list calls it "Key"...?
> > 
> > Did I hear the term "sub-addition" once, in passing?
> > 
> > 
> > 
> > On Tue, May 17, 2011 at 10:34 PM, Roger Hui 
> <[email protected]> 
> > wrote:> See  http://www.jsoftware.com/jwiki/Essays/Key
> > > for some additional uses of "key".
> > >
> > >
> > >
> > > ----- Original Message -----
> > > From: Marshall Lochbaum <[email protected]>
> > > Date: Tuesday, May 17, 2011 14:15
> > > Subject: Re: [Jprogramming] Aggregation
> > > To: 'Programming forum' <[email protected]>
> > >
> > >> The standard solution would be to use key (/.):
> > >> +//./ |: arr
> > >>
> > >> The three slashes in a row make this a bit confusing, but 
> it is
> > >> equivalentto
> > >> ({."1 (+/)/. {:"1) arr
> > >> where key is the really important part.
> > >>
> > >> Marshall
> > >>
> > >> -----Original Message-----
> > >> From: [email protected]
> > >> [mailto:[email protected]] On Behalf Of Ian Clark
> > >> Sent: Tuesday, May 17, 2011 5:09 PM
> > >> To: Programming forum
> > >> Subject: [Jprogramming] Aggregation
> > >>
> > >> I'm being lazy here. But I need a better answer than I can devil
> > >> out myself.
> > >> It occurs in a recent submission to Vector...
> > >>
> > >> I have an array like this (which may be unsorted, and can grow
> > >> very large):
> > >>
> > >> 1     100
> > >> 1     100
> > >> 1     20
> > >> 1     400
> > >> 2     30
> > >> 2     200
> > >> 2     300
> > >> 33    100
> > >> 33    100
> > >> 33    100
> > >>
> > >> I want to collapse it to:
> > >>
> > >> 1     620
> > >> 2     530
> > >> 33    300
> > >>
> > >> i.e. summing over subheadings.
> > >> The original example had A B C  in place of 1 2 33, but
> > >> numbers will do, to
> > >> save boxing. We don't know the full set of A B C ... in advance.
> > >> Nothing to
> > >> be assumed about the first column, except it is
> > >> +ve integers. But I'm also interested in the case where the first
> > >> column lies in the set: i.(n) for some n>0. In other words they
> > >> can be
> > >> squashed up.
> > >>
> > >> 1. There's simply got to be a "jem" to do it. Suggestions, 
> please.> >>       - Transpose the array if you wish. Box it: 1 
> 100 ; 1 
> > 100 ; 1 20 ;
> > >> ...
> > >> -whatever.
> > >>       - No, of course I don't want a looping solution :)
> > >>
> > >> 2. What do you call this process? I call it "aggregation" --
> but
> > >> I think the
> > >> name differs across disciplines.

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to