简体   繁体   English

从坐标数组中查找特定坐标的位置

[英]Finding the locations of specific coordinates from an array of coordinates

I have an array of coordinates as shown below:我有一个坐标数组,如下所示:

import numpy as np
values_to_test = np.array([[1 , 0],[2 , 0],[2 , 0.5],[1 , 0.5],[0 , 0],[2 , 1], [0 , 0.5],[1 , 1],[0 , 1]])

As my ultimate goal, I need to choose the positions from the values_to_test that contain the specific coordinates of the following array:作为我的最终目标,我需要从values_to_test中选择包含以下数组特定坐标的位置:

specific_values=np.array([[1,0],[0,0],[1,1]])

Hence, I'm expecting a result like: [True,False,False,False,True,False,False,True,False]因此,我期待这样的结果: [True,False,False,False,True,False,False,True,False]

I know if this was a 1D array I could use np.where but I cannot think about a way to handle this problem.我知道如果这是一个1D数组,我可以使用np.where但我想不出一种方法来处理这个问题。
Appreciate your help感谢你的帮助

You could make use ofnumpy.all and numpy.any like so:您可以像这样使用numpy.allnumpy.any

>>> (values_to_test[:, None] == specific_values).all(axis=-1).any(axis=-1)
array([ True, False, False, False,  True, False, False,  True, False])

This answer is a combination of using np.where , np.prod and list comprehension:这个答案是使用np.wherenp.prod和列表理解的组合:

values_to_test = np.array([[1 , 0],[2 , 0],[2 , 0.5],
                           [1 , 0.5],[0 , 0],[2 , 1], 
                           [0 , 0.5],[1 , 1],[0 , 1]])
specific_values=np.array([[1,0],[0,0],[1,1]])

output = [np.where(np.prod(values_to_test == x, axis = -1)) for x in specific_values]

Output: Output:

[(array([0], dtype=int64),),
 (array([4], dtype=int64),),
 (array([7], dtype=int64),)]

You can clean it up too with:您也可以使用以下方法进行清理:

output = [np.where(np.prod(values_to_test == x, axis = -1))[0][0] for x in specific_values]

[0, 4, 7] #output
import numpy as np
values_to_test = np.array([[1, 0], [2, 0], [2, 0.5], [1, 0.5], [
                          0, 0], [2, 1], [0, 0.5], [1, 1], [0, 1]])
specific_values = np.array([[1, 0], [0, 0], [1, 1]])

_1_j = np.array([[1], [1j]])

print([x in specific_values@_1_j for x in values_to_test@_1_j])
>>>[True, False, False, False, True, False, False, True, False]

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

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