June Kim asked:
> Is there an easier way to do the following? My first attempt was:
>    'day id val'=:|:(<a:;0 2)(".&.>@{`[`])} data
>    coord=:(<"1 day ,. (i.~ ~.) id)
>    load 'strings'
>    ysize=:>:>./day
>    (a:, <"1 ~.id), (<"0 i.ysize),.((<_);<a:) rplc~"1 
>    <"0 val coord} _$~ysize,#~.id

Part of the complexity you're wrestling with arises from your construction
of a boxed table with column headers and row labels.  Do you really need
that? Typically, that's only useful when you've done all your analysis and
computation and want to present a formatted result for human consumption. 
If "data" represents your input, to which you want to apply some analysis,
it would be simpler, easier, and faster to work with a pure (unboxed)
numeric matrix of values, then slap the headers on when you're done.

If you really need the formatted boxed table, then using amend, as you do,
is the most common and straightforward approach. I can't really recommend
many improvements, except perhaps to construct the body of the table
directly, and skip the "rplc" part.  For example:

           coord2tab =: verb define
                'x id d'=.<"1 |: y
        
                my =. >./ y =. id i.~ uid =. ~.id
                mx =. >./ x =.  0 ". ;:^:_1 x
        
                t  =. d (x ;&.> y)} (1 + mx,my) $ a:
                (a: , <"0 i. 1+mx) ,. uid , t 
        )


           data=: TAB cut&> LF cut noun define-.' '
                 0      p1       4
                 2      p1       6
                 3      p1      10
                13      p1      15
                 0      p2       3
                 1      p2       5
                 7      p2      10
                10      p2      11
                14      p2      12
                 1      p3       9
                 5      p3      10
                 9      p3      20
                 7      p4      10
        )
   

           coord2tab data 
        +--+--+--+--+--+
        |  |p1|p2|p3|p4|
        +--+--+--+--+--+
        |0 |4 |3 |  |  |
        +--+--+--+--+--+
        |1 |  |5 |9 |  |
        +--+--+--+--+--+
        |2 |6 |  |  |  |
        +--+--+--+--+--+
        |3 |10|  |  |  |
        +--+--+--+--+--+
        |4 |  |  |  |  |
        +--+--+--+--+--+
        |5 |  |  |10|  |
        +--+--+--+--+--+
        |6 |  |  |  |  |
        +--+--+--+--+--+
        |7 |  |10|  |10|
        +--+--+--+--+--+
        |8 |  |  |  |  |
        +--+--+--+--+--+
        |9 |  |  |20|  |
        +--+--+--+--+--+
        |10|  |11|  |  |
        +--+--+--+--+--+
        |11|  |  |  |  |
        +--+--+--+--+--+
        |12|  |  |  |  |
        +--+--+--+--+--+
        |13|15|  |  |  |
        +--+--+--+--+--+
        |14|  |12|  |  |
        +--+--+--+--+--+
           
But as I said, this is fundamentally the same approach, with minor
simplification and some reformatting of the code.

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

Reply via email to