How to make the following data in a box from a list

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 it? I would really appreciate your help.

+4
source share
2 answers

You need to call gerund in add mode ( :0 ).

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

If you need them in a box, simply ;/ 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│ └─┴─┘ 
+1
source

If you need vertical (column-oriented) output, you must first convert your list to vertical (in the column) format:

  ,.1 2 3 1 2 3 

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

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

Two notes:

  • It is optional and not necessary to use fields in J when all your data is of the same type and shape. In your case, both of your operations are scalar and numeric, so both verbs will generate a column of 1x3 numbers. It would be more typical and idiomatic to combine them with,. to create a 2x3 digit table than ; to create 2 opaque blocks.

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

+1
source

Source: https://habr.com/ru/post/1492506/


All Articles