【发布时间】:2017-03-11 20:05:33
【问题描述】:
我有两个输入 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,则基本上不可能有效地完成它一次替换一对元素。
标签: python arrays function loops numpy