给定行和列索引,分配到NumPy数组的网格中
问题描述:
我想访问2d numpy数组的特定行和列限制.
I want to access a specific row and column restriction of a 2d numpy array.
> x
array([[1, 2, 0],
[3, 4, 0],
[0, 0, 1]])
如果我做自然的事情,我只会得到受限数组的对角线元素.
If I do what seems natural, I just get the diagonal elements of the restricted array.
> x[[0,1], [0,1]]
array([1, 4])
相反,我可以这样做来阅读我想要的内容-
Instead I can do this to read what I want -
> x[[0,1],:][:,[0,1]]
array([[1, 2],
[3, 4]])
..但是它不允许我编写/分配值.
..but it doesn't let me write/assign the values.
> x[[0,1],:][:,[0,1]] = np.array([[1,0],[0,1]])
> x
array([[1, 2, 0],
[3, 4, 0],
[0, 0, 1]])
如何在此处写入矩阵?