二维数组,不等式和函数

时间:2021-09-12 21:40:19

If given an array like

如果给出一个类似的数组

a = array([[2,4,9,8,473],[54,7,24,19,20]])

then how can I write the indexes of the array which are between values x and y?

那么如何编写值x和y之间的数组索引?

currently I've got:

目前我有:

where(5 > a > 10)

if will however give an output if I say for example:

但是如果我说的话会给出一个输出:

where(a > 5)

but the where function doesn't take this command and once it will it should output a 2 one dimensional array, is there a way to easily stack them?

但是where函数不接受这个命令,一旦它输出一个2维一维数组,有没有办法轻松堆叠它们?

2 个解决方案

#1


1  

You can use logical operator &(and) | (or) to chain different conditions together, so for your case, you can do:

您可以使用逻辑运算符&(和)| (或)将不同条件联系在一起,因此对于您的情况,您可以:

np.where((a > 5) & (a < 10))

# (array([0, 0, 1]), array([2, 3, 1]))
# here np.where gives a tuple, the first element of which gives the row index, while the 
# second element gives the corresponding column index

If you want the indices to be an array where each row represents an element, you can stack them:

如果您希望索引是一个数组,其中每行代表一个元素,您可以堆叠它们:

np.stack(np.where((a > 5) & (a < 10)), axis=-1)
# array([[0, 2],
#        [0, 3],
#        [1, 1]])

Or as @Divakar commented use np.argwhere((a > 5) & (a < 10)).

或者@Divakar评论使用np.argwhere((a> 5)&(a <10))。

#2


0  

you have two indexes that you need to specify, one for which inner array you are referencing and the other for what actual member of that array you are referring to

您需要指定两个索引,一个是您要引用的内部数组,另一个是您引用的该数组的实际成员

#1


1  

You can use logical operator &(and) | (or) to chain different conditions together, so for your case, you can do:

您可以使用逻辑运算符&(和)| (或)将不同条件联系在一起,因此对于您的情况,您可以:

np.where((a > 5) & (a < 10))

# (array([0, 0, 1]), array([2, 3, 1]))
# here np.where gives a tuple, the first element of which gives the row index, while the 
# second element gives the corresponding column index

If you want the indices to be an array where each row represents an element, you can stack them:

如果您希望索引是一个数组,其中每行代表一个元素,您可以堆叠它们:

np.stack(np.where((a > 5) & (a < 10)), axis=-1)
# array([[0, 2],
#        [0, 3],
#        [1, 1]])

Or as @Divakar commented use np.argwhere((a > 5) & (a < 10)).

或者@Divakar评论使用np.argwhere((a> 5)&(a <10))。

#2


0  

you have two indexes that you need to specify, one for which inner array you are referencing and the other for what actual member of that array you are referring to

您需要指定两个索引,一个是您要引用的内部数组,另一个是您引用的该数组的实际成员