通过测试numpy数组中的每个元素是否在2个数字之间来创建布尔数组 [英] Creating a boolean array by testing if each element in numpy array is between 2 numbers
问题描述
我有一个数字的numpy数组,我想创建一个具有相同大小和尺寸的布尔数组,该数组说明该元素是否位于2个数字之间.例如:
I have a numpy array of numbers and i want to create a boolean array of the same size and dimensions that says whether or not that element lies between 2 numbers. For example:
a=np.array([[1,2,3],[4,5,6],[7,8,9]])
我知道我是否写信,
print a>3
我得到一个数组,该数组的前三个元素为"False",其余的为"True"
I get an array that has the first three elements "False" and the rest "True"
np.array([[False,False,False],[True,True,True],[True,True,True]])
但是我想得到一个条件满足的布尔数组
But i want to get a boolean array where the conditions are such that
a>3 and a<8
有没有一种方法,而无需在for循环中逐个检查每个元素?我的数组是2048 * 2048,并且花费的时间太长了
Is there a way to do this without checking every element one by one in a for loop? I have arrays that are 2048*2048 and that takes too long
有人报告说,这与另一个解决方案使用numpy.where函数的问题相同,但是返回条件为true的元素,我的问题是返回布尔值
Some has reported that is is the same question as another where the solution was to use the numpy.where function, but that returns the elements where a condition is true, my question is to return booleans
推荐答案
您可以检查以下范围:
print (3<a)&(a<8)
这篇关于通过测试numpy数组中的每个元素是否在2个数字之间来创建布尔数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!