Question

In J, I have a list like 1 2 3, I want to take *: and 2* to make it a box structure like

┌─┬─┐
│1│2│
│4│4│
│9│6│
└─┴─┘

or even

┌───┐
│1 2│
├───┤
│4 4│
├───┤
│9 6│
└───┘

How to do this? I would really appreciate your help.

Était-ce utile?

La solution

You need to evoke a gerund in append mode (:0).

a =: 1 2 3
*:`+: (`:0) a
1 4 9
2 4 6

If you need them boxed, just ;/ them:

;/*:`+: (`:0) a
┌─────┬─────┐
│1 4 9│2 4 6│
└─────┴─────┘
;/ |:  *:`+: (`:0) a      NB. different axis
┌───┬───┬───┐
│1 2│4 4│9 6│
└───┴───┴───┘
 _3 <\6 1 $,*:`+: (`:0) a   NB. yet another axis
┌─┬─┐
│1│2│
│4│4│
│9│6│
└─┴─┘

Autres conseils

If you want vertical (column-oriented) output, you should first convert your list to vertical (column-oriented) format:

    ,.1 2 3
 1
 2
 3

Now you can apply as many calculations as you like. If you want to put each output in a separate box, you can chain them together using ; in a fork:

    (*: ; 2&*) ,. 1 2 3

Two notes:

  • It is unusual, and unnecessary, in J, to use boxes when all your data is the same type and shape. In your case, both your operations are scalar and numeric, and so both verbs will produce a 1x3 column of numbers. It would be more typical and idiomatic to combine them with ,. to produce a 2x3 table of numbers than ; to produce 2 opaque boxes.

  • "Double" is already a primitive in J, so instead of 2&*, we could say +: as in (*: ; +:) ,. 1 2 3

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top