我有两个输入的numpy数组,分别是一组点的纬度和经度坐标:lats
和lons
。
我继承了一个将每个(lat,lon)
对转换为(E,N)
对的函数:
def convert(lat,lon): #takes two floats as arguments (unit: degrees)
...
computation #Actual function is too long to post
...
return N,E #returns two floats (unit: meters)
我的问题:如何高效地同时将相同的函数应用于两个输入的numpy数组?
我考虑修改该函数,使其返回一个列表:
return [N,E]
以这种方式:
rows = int(lat.shape[0]) #lat and lon have the same shape
cols = int(lat.shape[1])
easting=numpy.zeros(shape=(rows,cols))
northing=numpy.zeros(shape=(rows,cols))
for i in range(0, rows):
for j in range(0, cols):
northing=convert(lon[i][j])[0] #first element of the returned list
easting=convert(lat[i][j])[1] #second element of the returned list
我还没有测试过,但是通过观察,我不太确定这个能否正常工作。非常感谢任何见解。
convert
函数的细节。它可能已经按照你想要的方式正常工作,或者你可能需要将math.sin
切换为np.sin
之类的操作,或者如果代码依赖于无法控制或替换的API,并且每次只处理一对元素,那么有效地完成此操作可能基本上是不可能的。 - undefined