操作 numpy 矩阵..替换一个值
Manipulating numpy matrices..replacing a value
我最近发现了 numpy 及其在矩阵方面的实用性。有一件事仍然困扰着我。如何用所需值替换矩阵中的特定值。我试过了>
matrix1 = np.zeros([10,10])
replacewith = 1
atposition = np.array(5,5)
matrix1[atposition] = replacewith
所以我想做的是用 1 替换中间的 0。
谢谢!
import numpy as np
matrix1 = np.zeros([10,10])
replacewith = 1
atposition = (5,5)
matrix1[atposition] = replacewith
print matrix1
输出:
[[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
我最近发现了 numpy 及其在矩阵方面的实用性。有一件事仍然困扰着我。如何用所需值替换矩阵中的特定值。我试过了>
matrix1 = np.zeros([10,10])
replacewith = 1
atposition = np.array(5,5)
matrix1[atposition] = replacewith
所以我想做的是用 1 替换中间的 0。 谢谢!
import numpy as np
matrix1 = np.zeros([10,10])
replacewith = 1
atposition = (5,5)
matrix1[atposition] = replacewith
print matrix1
输出:
[[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]