我有兩個輸入numpy數組,分別具有一組點的經度和緯度座標:lats
和lons
。Numpy:將函數應用於兩個numpy數組並返回兩個numpy數組
我繼承每個(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'功能的細節。它可能已經按照你想要的方式工作了,或者你可能不得不把它們切換成'np.sin',否則如果代碼依賴於你可以使用的API不能一次控制或替換一對元素。 – user2357112