开发者

Force array instead of matrix in J for "i."

The i. primitive produces a list 开发者_C百科of integers:

   i. 10
0 1 2 3 4 5 6 7 8 9

If I want to produce several short lists in a row, I do this:

   ;i."0 each [ 2 3 4
0 1 0 1 2 0 1 2 3

(the result I want)

Boxing (that each) is a crutch here, because without it, i."0 produces a matrix.

   i."0 [ 2 3 4
0 1 0 0
0 1 2 0
0 1 2 3

(the result I don't want)

Is there a better way to not have i."0 format the output to a matrix, but an array?


No, I believe you can't do any better than your current solution. There is no way for i."0 to return a vector.

The "0 adverb forces i. to accept scalars, and i. returns vectors. i. has no way of knowing that your input was a vector rather than a scalar. According to The J primer the result shape is the concatenation of the frame of the argument and the result.

The shortest "box-less" solution I've found so far is

(*@$"0~#&,i."0) 2 3 4

which is still longer than just using ;i. each 2 3 4

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜