我有一个numpy数组arrayBig
10 5 27 30 34
2 34 23 2 3
2 3 43 12 23
2 24 34 2 34
我有一个numpy数组arraySmall
1 0 0
0 1 1
1 1 0
我想要的是一个numpy数组arrayNew
34 0 0
0 43 12
24 34 0
我知道我的arraySmall
形状为(3,3)
,位于(1 1)
的索引arrayBig
。如何使用Numpy获取arrayNew
?
答案 0 :(得分:2)
>>> import numpy as np
>>> arrayBig = np.array([
... [10, 5, 27, 30, 34],
... [2, 34, 23, 2, 3],
... [2, 3, 43, 12, 23],
... [2, 24, 34, 2, 34],
... ])
>>> arraySmall = np.array([
... [1, 0, 0],
... [0, 1, 1],
... [1, 1, 0],
... ])
>>> arrayBig[1:4, 1:4] * arraySmall
array([[34, 0, 0],
[ 0, 43, 12],
[24, 34, 0]])
答案 1 :(得分:1)
我recently learned about advanced boolean indexing。我不确定它是否比其他答案更好,但你可以这样做:
>>> a = np.array([[1,2,3],[4,5,6]])
>>> b = np.array([[1,0],[0,1]])
>>> c = b == 0
>>> d = a[0:b.shape[0],0:b.shape[1]]
>>> d[c] = 0
>>> d
array([[1, 0],
[0, 5]])