【发布时间】:2023-03-13 19:03:01
【问题描述】:
我正在尝试更新 numpy 矩阵的一组特定行和列。这是一个例子:
import numpy as np
A=np.zeros((8,8))
rows=[0, 1, 5]
columns=[2, 3]
#(What I am trying to achieve) The following does not update A
A[rows][:,columns]+=1
#while this just does
for i in rows:
A[i][columns]+=1
我期望的输出是:
In [1]:print(A)
Out[1]:
array([[ 0., 0., 1., 1., 0., 0., 0., 0.],
[ 0., 0., 1., 1., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 1., 1., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0.]])
有没有办法在不循环的情况下同时执行多列和-行更新?
【问题讨论】: