那docs调用填充val
标量是一个现有的文档错误。事实上,任何可以在这里广播的值都可以。
填充对角线适用于类似数组的情况:
>>> a = np.arange(1,10).reshape(3,3)
>>> a
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
>>> np.fill_diagonal(a, [99, 42, 69])
>>> a
array([[99, 2, 3],
[ 4, 42, 6],
[ 7, 8, 69]])
这是一个跨步技巧,因为对角线元素的间距是数组的宽度 + 1。
从文档字符串来看,这是一个比使用更好的实现np.diag_indices
too:
Notes
-----
.. versionadded:: 1.4.0
This functionality can be obtained via `diag_indices`, but internally
this version uses a much faster implementation that never constructs the
indices and uses simple slicing.