Page 1 of 1

Why does decode of a matrix work by column and not row

Posted: Sun Feb 25, 2024 7:38 pm
by jmosk
I have matrix ← 4 3 ⍴ 0 0 0 1 1 0 0 1 1 1 1 1

Code: Select all

0 0 0
1 1 0
0 1 1
1 1 1
I wish to convert each row from binary to decimal with the ⊥ primitive. The result is

Code: Select all

2⊥ matrix
5 7 3
I thought/expected to get 0 6 3 7. I had to transpose the matrix to get what I thought.

Code: Select all

2⊥⍉matrix
0 6 3 7
Yet if I sum reduce the matrix, it operates by rows

Code: Select all

+/matrix
0 2 2 3
So why does the ⊥ seem to function on rank 2, while the / functions on rank 1? There is a separate ⌿ function to operate on rank 2.

Code: Select all

+⌿matrix
2 3 2
This seems inconsistent between the two primitives with one operating column-wise and the other row-wise.

Re: Why does decode of a matrix work by column and not row

Posted: Mon Feb 26, 2024 9:42 am
by Phil Last
In fact decode works not on rank 2 but on the first dimension. The fact that reduction works on the last dimension is a mistake or oversight made in the earliest impementations and only corrected by Ken and Roger when they implemented J.

So the why is history.

Re: Why does decode of a matrix work by column and not row

Posted: Mon Feb 26, 2024 11:10 am
by yaemmanuelli
Hello,
Decode syntax is derived from internal product syntax which operates on last dimension of left argument and first dimension of right argument.
Regards.
--
Yves-Antoine Emmanuelli

Re: Why does decode of a matrix work by column and not row

Posted: Sun Mar 03, 2024 5:43 am
by Adam|Dyalog
To summarise previous posts:
  1. Regard ⌿ as the primary form and works along the leading axis, and / is convenient name for ⌿⍤1 which works along the trailing axis
  2. ⊥ is a convenient wrapper on +.× namely {⍵+.×⍨⌽×\1,⍺⍴⍨¯1+≢⍵} making it work along the leading axis
And to fix your issue:
  • As with ⌿ you can make ⊥ work along the trailing axis with ⍤1:

    Code: Select all

          2(⊥⍤1)matrix
    0 6 3 7