[英]Put zeros in the given index values of a 2D array in Python
I have arrays 我有数组
A = np.array([
[1,6,5],
[2,3,4],
[8,3,0]
])
B = np.array([
[3,2,1],
[9,2,8],
[2,1,8]
])
Doing a = np.argwhere(A > 4)
gives me an array of position/indexes of values in array A that are greater than 4 ie [[0 1], [0 2], [2 0]]
. 进行
a = np.argwhere(A > 4)
给我一个数组A的值的位置/索引,该数组大于4,即[[0 1], [0 2], [2 0]]
。
I need to use these indexes/position from a = np.argwhere(A > 4)
to replace the values in array B to zero at these specific position ie array B should now be 我需要从
a = np.argwhere(A > 4)
使用这些索引/位置,以将数组B中的值在这些特定位置替换为零,即数组B现在应该为
B = np.array([
[3,0,0],
[9,2,8],
[0,1,8]
])
I am big time stuck any help with this will be really appreciated. 我辛苦了,为此提供的任何帮助将不胜感激。
Thank You :) 谢谢 :)
它应该很简单:
B[A > 4] = 0
In general, though, note that the indices returned by np.where
are meant to be applied to numpy.ndarray
objects, so you could have done: 但是,一般来说,请注意,
np.where
返回的索引旨在应用于numpy.ndarray
对象,因此您可以这样做:
B[np.where(A > 4)] = 0
Generally I don't use np.where
with a condition like this, I just use the boolean mask directly, as in John Zwinck's answer. 通常我不使用
np.where
的条件,我只是直接使用布尔型掩码,如John Zwinck的回答。 But it is probably important to understand that you could 但是了解您可以
>>> B[np.where(A > 4)] = 0
>>> B
array([[3, 0, 0],
[9, 2, 8],
[0, 1, 8]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.