2

(Dyalog) APL 中是否有一个简单的习惯用法来用零包围数字数组?

目前对于等级为 1 的数值数组NA,或我使用的向量

0,⍨0, NA

对于排名为 2 的数组,我使用

0,[1]⍨0,[1]0,⍨0, NA

对于更高的等级,我可以重复这种模式,但我确信必须有更好的方法来做到这一点(并且对所有等级都有一个定义),但我就是想不出一个似乎没有的方法过于复杂。

4

5 回答 5

2

(¯2-⍴)↑(1+⍴)↑⊢­­­­­­­­­­­­­­­

于 2018-06-05T18:27:28.320 回答
1
于 2018-06-05T17:11:46.897 回答
1
{r←≢⍴⍵ ⋄ i←1⌽⍳r ⋄ {0⍪0⍪⍨i⍉⍵}⍣r⊢⍵}

⍴⍵ the shape of the array
≢</code> the number of elements of that (i.e. array's rank)
r←</code> store that as r (for rank)

⍳r the ɩndices of the rank
1⌽</code> rotate them one step
i←</code> store in i (for indices)

⊢⍵ on the array
{…<code>}⍣r
 apply the following function r times (i.e. once per dimension):
 <code>i⍉⍵
 reorder the axes of the argument according to i (i.e. put the next dimension in front)
 <code>0⍪⍨ append with 0s along the first axis
 <code>0⍪</code> prepend with 0s along the first axis

In other words, each axis gets its turn at being in front, where we prepend and append 0s.

Try it online!

于 2018-06-05T17:38:26.653 回答
1
于 2018-06-05T18:06:22.063 回答
0

所以,我已经将我的方法推广到了一个递归 dfn,它满足了我的“适用于任意等级”的要求,但我对此并不满意,因为我仍然认为有一种更好的方法可以做到这一点,而我只是错过了。我把它作为答案,因为它在技术上回答了我的问题,但我仍在寻找更好的解决方案。

{(⍴⍴⍵){⍺=0:⍵ ⋄ (⍺-1)∇0,[⍺]⍨0,[⍺]⍵}⍵}
于 2018-06-05T16:59:52.063 回答