问题
I have a n-by-m matrix written at paper and I want to make calculations on it in J.
I can enter matrices like that (n = 3, m = 3):
A =: 3 4 $ 1 3 2 4 7 8 1 2 2 0 0 1
The question is how to enter a matrix like that:
A =: (something here)
1 3 2 4
7 8 1 2
2 0 0 1
)
The reason why I'm asking such a weird question is that I have seen in some book a similar method of typing matrices using verb 0 : 0
or something alike, but I cannot remember where. :{
回答1:
I use (something here)
as (".;._2) 0 : 0
. That way I can also mix expressions.
] 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
回答2:
The use of dyadic (rather than monadic) ".
will attempt to resolve each line as a string of numbers rather than evaluate it as a J sentence. Where a string cannot be resolved to a number, the left argument (_99
in the example below) is used instead. This approach will correctly interpret a larger set of numbers represented as strings.
The choice of noun define
rather than 0 : 0
simply saves a set of brackets that is otherwise needed to separate the _2
from the 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
来源:https://stackoverflow.com/questions/26241527/j-handy-method-to-enter-a-matrix