简体   繁体   English

通过测试numpy数组中的每个元素是否在2个数字之间来创建布尔数组

[英]Creating a boolean array by testing if each element in numpy array is between 2 numbers

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. 我有一个数字的numpy数组,我想创建一个大小和尺寸相同的布尔数组,该数组说明该元素是否位于2个数字之间。 For example: 例如:

a=np.array([[1,2,3],[4,5,6],[7,8,9]])

I know if I write, 我知道如果我写

print a>3

I get an array that has the first three elements "False" and the rest "True" 我得到一个具有前三个元素“ False”和其余“ 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

Is there a way to do this without checking every element one by one in a for loop? 有没有一种方法无需在for循环中逐个检查每个元素? I have arrays that are 2048*2048 and that takes too long 我的数组是2048 * 2048,花费的时间太长

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 有人报告说,这与另一个解决方案是使用numpy.where函数的问题相同,但是返回条件为true的元素,我的问题是返回布尔值

您可以检查以下范围:

print (3<a)&(a<8)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM