我想将零分配给 2d numpy 矩阵,其中对于每一行我们都有一个截止列,之后应将其设置为零。
例如,这里我们有大小为 4x5 的矩阵 A,带有截止列 [1,3,2,4]。我想做的事:
import numpy as np
np.random.seed(1)
A = np.random.rand(4, 5)
cutoff = np.array([1,3,2,4])
A[0, cutoff[0]:] = 0
A[1, cutoff[1]:] = 0
A[2, cutoff[2]:] = 0
A[3, cutoff[3]:] = 0
我可以用 np.repeat 行索引和列来做到这一点,但我的矩阵太大了,我做不到。有没有一种有效的方法来做到这一点?