【发布时间】:2016-08-15 11:30:05
【问题描述】:
我正在尝试为给定的一对 X 和 Y 找到最近点以访问其值。就我而言,在 X 方向 (np.arrange(0,X.max(),1),我想从 Y = 0 中提取最接近的值,以获取其在“值数组”中的值:
import numpy as np
import matplotlib.pyplot as plt
#My coordinates are given here :
X = np.array([0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4 ,5, 0, 1, 2, 3, 4 ,5])
Y = np.array([-2.5, -1.5, 0, 0, 2, 2.5, 2, 1.5, 1, -1, -1.2,-2.5, 0.2, 0.5, 0, -0.5, -0.1,0.05])
plt.scatter(X,Y);plt.show()
#The corresponding values are :
values = np.array([-1.1, -9, 10, 10, 20, 25, 21, 15, 0, 2, -2,-5, 2, 50, 0, -5, -1,5])
# I thought to use a for loop :
def find_index(x,y):
xi=np.searchsorted(X,x)
yi=np.searchsorted(Y,y)
return xi,yi
for i in arange(float(0),float(X.max()),1):
print i
thisLat, thisLong = find_index(i,0)
print thisLat, thisLong
values[thisLat,thisLong]
但我得到一个错误:“IndexError: too many indices”
【问题讨论】:
-
检查一下看看是否有帮助:stackoverflow.com/questions/29199585/…