如何找到与给定坐标对应的二维网格的索引?

时间:2018-08-13 19:33:56

标签: python numpy coordinates

因此,尝试在网格网格(两个2d数组)上搜索坐标的位置时遇到麻烦。一个简单的示例如下:

>>> x = [1,2,3]  
    y = [4,5,6]
    xx,yy = np.meshgrid(x,y)

    coord = [1,5] #the coordinate I am looking for

    print(xx)
    print(yy)

[[1 2 3]
 [1 2 3]
 [1 2 3]]

[[4 4 4]
 [5 5 5]
 [6 6 6]]

我希望代码返回coord_idx = [1,0],即存储坐标的索引,以便[xx[coord_idx],yy[coord_idx]]返回[1,5]

1 个答案:

答案 0 :(得分:0)

尝试一下:

import numpy as np

x = [1,2,3]
y = [4,5,6]
xx,yy = np.meshgrid(x,y)

coord = [1,5]

coord_idx = np.argwhere((xx==coord[0]) & (yy==coord[1]))[0]