一个可以用bincount
and outer
>>> M - np.outer(V, np.bincount(I, None, M.shape[1]))
array([[ 0, 1, 0, -3],
[ 1, 0, 0, -3],
[ 0, 0, 1, -3],
[ 1, 0, 0, -3],
[ 0, 0, 0, -2]])
or subtract.at
>>> out = M.copy()
>>> np.subtract.at(out, (np.s_[:], I), V[:, None])
>>> out
array([[ 0, 1, 0, -3],
[ 1, 0, 0, -3],
[ 0, 0, 1, -3],
[ 1, 0, 0, -3],
[ 0, 0, 0, -2]])