You can do this -
import numpy as np
mat=np.array([[5,5,3,6,3],
[3,2,7,8,1],
[7,5,5,2,0]])
mat = np.hstack((mat, np.ones((3,3))*np.nan))
vec=np.array([3,1,2])
idx = vec[:, None] + np.arange(0, 3)
print(mat[np.arange(3)[:,None], idx])
Gives -
[[ 6. 3. nan]
[ 2. 7. 8.]
[ 5. 2. 0.]]
First just append the original array with three columns of inf
or None
or something. Then create a 2d index array from the vec
by adding sequential integers from 0 and simply index the original matrix.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…