【发布时间】:2018-08-29 12:22:49
【问题描述】:
我想将零分配给 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 行索引和列来做到这一点,但我的矩阵太大了,我做不到。有没有有效的方法来做到这一点?
【问题讨论】:
标签: numpy matrix-indexing numpy-indexing