The most concise J syntax for creating a numerical matrix

Imagine that I want to take numbers from 1 to 3 and form a matrix so that every possible pairing is represented, for example,

eleven
12
thirteen
2 1
2 2
2 3
3 1
3 2
3 3

Here is the monadic verb I formulated in J to do this:

($ ~ (-: @ #, 2 :)),, "0 / ~ 1 + iy

Initially, I thought that ,"0/~ 1+i.ywould be enough, but, unfortunately, it produces the following output:

eleven
12
thirteen

2 1
2 2
2 3

3 1
3 2
3 3

In other words, its form is 3 3 2, and I want something whose shape is 9 2. The only way I could fix it would be to pour all the data into a new form. I am convinced that there must be a more concise way of doing this. Somebody knows?

+3
source share
3 answers

( { ):

>,{ 2#<1+i.y

EDIT:

:

:

>,{ y#<1+i.y

:

>,{ x#<1+i.y
+4

. ,/, ,/ ,"0/~ 1+i.y

+5

I understand this question is old, but there is an easier way to do this: count to 9 in trinary and add 1.

   1 + 3 3 #: i.9
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3

3 3 & #:gives two numbers. General verb "base 3" 3 & #.^:_1.

+2
source

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


All Articles