【发布时间】:2015-09-08 14:30:07
【问题描述】:
我必须在 python 的 2 个数组中找到 2 个不同的值。它不会给我这样的问题,但 2 个值必须在相同的 x,y 坐标中,我不知道如何处理。
例如:我必须在第一个数组中找到最接近的数字 5,在第二个数组中找到最接近的数字 87,但 x 和 y 必须具有相同的值。
目前,我已经计算了每个数组的两个 x 和 y 值的平均值。但结果根本不准确。
你有什么想法可以解决这个问题吗?
编辑: 有我的代码:
#This is two arrays that I extract from a file. It contains a lot of values corresponding to latitude and longitude on a earth's photograph.
latitude = data.get_band('latitude').read_as_array(width, height, 0, 0)
longitude = data.get_band('longitude').read_as_array(width, height, 0, 0)
idx = (np.abs(latitude-lat0)).argmin()
nearest_lat = latitude.flat[idx]
idx_lat = np.unravel_index(idx, latitude.shape)
idx = (np.abs(longitude-lon0)).argmin()
nearest_lon = longitude.flat[idx]
idx_lon = np.unravel_index(idx, longitude.shape)
编辑 2: 最后,我使用了另一种方法:
latitude = data.get_band('latitude').read_as_array(width, height, 0, 0)
longitude = data.get_band('longitude').read_as_array(width, height, 0, 0)
dist = (latitude - lat0)**2 + (longitude - lon0)**2
idx = np.argmin(dist)
idx = np.unravel_index(idx, latitude.shape)
PIXELCOL = idx[0]
PIXELROWS = idx[1]
感谢大家的帮助!
【问题讨论】:
-
我不明白你所说的“这两个值必须在同一个 x,y 坐标中”是什么意思
-
欢迎来到 StackOverflow。要改进您的chances to get useful answers,请show us your code。
-
x,y和这两个数组以及两个不同的数字有什么关系?!!
-
我有两个数组和两个代表地理坐标的不同数字。一个值代表一个像素。我必须从用户输入的像素中选择一个值最接近的像素