J: A convenient method for entering a matrix?
I have an n-by-m matrix written on paper and I want to do calculations on it in J.
I can enter matrices like this (n = 3, m = 3):
A =: 3 4 $ 1 3 2 4 7 8 1 2 2 0 0 1
The question is how to introduce a matrix like this:
A =: (something here)
1 3 2 4
7 8 1 2
2 0 0 1
)
The reason I am asking such a strange question is because I have seen in some book a similar method for typing matrices using a verb 0 : 0
or something similar, but I cannot remember where. : {
I am using (something here)
both (".;._2) 0 : 0
. This way I can mix expressions as well.
] A =: (".;._2) 0 : 0
5 $ 0
? 5 $ 5
5 $ 1
1 2 3 2 1
)
0 0 0 0 0
2 1 0 0 3
1 1 1 1 1
1 2 3 2 1
Using a dyadic (rather than monadic) ".
will try to resolve each string as a string of numbers, rather than evaluate it as a J-sentence. If the string cannot be resolved to a number, the left argument is used instead ( _99
in the example below). This approach will correctly interpret a larger set of numbers represented as strings.
Choice noun define
, rather than 0 : 0
just storing the set of brackets that would otherwise be needed to split _2
by 0
.
]A =: _99&".;._2 noun define
1 -3 2 4
7 8 1 2
2 0 0 1
)
1 _3 2 4
7 8 1 2
2 0 0 1