[英]How do I find the index of a 2d meshgrid that corresponds to a given coordinate?
So, I'm having trouble trying to search for the location of a cordinate on a meshgrid (two 2d arrays). 因此,在尝试在网格网格(两个2d数组)上搜索坐标的位置时遇到了麻烦。 A simple example looks like:
一个简单的示例如下所示:
>>> 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]]
I would like the code to return coord_idx = [1,0]
, the index where the coordinate is stored, such that [xx[coord_idx],yy[coord_idx]]
returns [1,5]
. 我希望代码返回
coord_idx = [1,0]
(存储坐标的索引),以便[xx[coord_idx],yy[coord_idx]]
返回[1,5]
。
Try this: 尝试这个:
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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.