2008/5/18, Roger Hui <[EMAIL PROTECTED]>:
> Convert areas and models into indices and assign sales
> into those locations of a matrix of zeros.
>
> ij=. area,.model
> (ij +//. sales) (<"1 ~.ij)}(m,n)$0
Thanks.
It is basically similar to what I originally wrote, but yours is much cleaner.
Hence, I have cleaned up my code as following:
load 'strings'
data=: 0 : 0
area model sales
north T2 3
south T2 4
north T3 10
north T3 5
east T1 1
west T3 1
south T2 11
north T4 2
west T3 1
east T4 1
west T4 6
)
d=: (<a:;<2) ((".&.>)@{)`[`]} }. cut;._2 data
ind=:i.~ ~.
areas=: ind 0{"1 d
models=:ind 1{"1 d
sales=: > 2{"1 d
keys=:areas,.models
salessum=:keys +//. sales
blank=:0$~(#~.areas),#~.models
salessum (<"1 ~. keys)} blank
3 15 0 2
15 0 0 0
0 0 1 1
0 2 0 6
Any more suggestions for improvement?
>
> If there can be at most one figure for each
> pair of (area,model) then you can skip the
> cumulation and the nub:
>
> sales (<"1 area,.model)}(m,n)$0
>
Right, but it's not appicable to my case.
>
>
> ----- Original Message -----
> From: June Kim <[EMAIL PROTECTED]>
> Date: Saturday, May 17, 2008 7:44
> Subject: [Jprogramming] 2-dimensional summary report
> To: Programming forum <[email protected]>
>
> > Suppose the data is given as following:
> >
> > data=: 0 : 0
> > area model sales
> > north T2 3
> > south T2 4
> > north T3 10
> > north T3 5
> > east T1 1
> > west T3 1
> > south T2 11
> > north T4 2
> > west T3 1
> > east T4 1
> > west T4 6
> > )
> > d=: (<a:;<2) ((".&.>)@{)`[`]} }. cut;._2 data
> >
> >
> > What I want is a report of sales for area by model:
> >
> >
> > NB. T2 T3 T1 T4
> > NB. north 3
> > 15 0 2
> > NB. south 15
> > 0 0 0
> > NB. east
> > 0 0 1 1
> > NB. west
> > 0 2 0 6
> >
> >
> > The final matrix doesn't need to contain row, column-headers --
> > it is
> > fine that it contains only the sales total for each area and model
> > pairs.
> >
> > I'd like to know easy and simple ways to getting that matrix in J.
> > What's your suggestion?
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm