我有下面的python代码
import numpy as np
II = np.eye(3, 3, k=-1)
II[0,0] = 0
II[0,1] = 0
print(II)
得到了结果
[[ 0. 0. 0.]
[ 1. 0. 0.]
[ 0. 1. 0.]]
我想要的预期结果是
[[ 0. 0. 0.]
[ -1. 1. 0.]
[ 0. -1. 1.]]
我该如何解决?
答案 0 :(得分:1)
您可以执行以下操作。
import numpy as np
A = np.eye(3, 3)
B = np.eye(3, 3, k=-1)
B[B>0]=-1
C = A + B
C[0, 0] = 0
print(C)
输出:
[[ 0. 0. 0.]
[-1. 1. 0.]
[ 0. -1. 1.]]