我有三個相同形狀的2D陣列,讓我們稱它們為θ,phi和A。設θ和phi是與從表面上不同距離看到的法向量的角度:size = 100 # this value is fixedx = np.arange(-size, size)y = np.arange(-size, size)xx, yy = np.meshgrid(xx, yy)theta = np.arctan((xx**2+yy**2)**0.5 / 100) # angle from distance 100phi = np.arctan((xx**2+yy**2)**0.5 / 1000) # angle from distance 1000設 A 是測量值的 2D 映射,其中 x 軸是 θ,y 軸是 phi,在已知和線性步長中(實際上與 θ 和 phi 的形狀不同)。我需要的是用A(x,y)表示的A(theta,phi)的值。似乎我不知道如何將A(theta,phi)轉換為A(x,y),即使我知道theta(x,y)和phi(x,y)。我嘗試了什么: 通過 scipy.interpolate.interp2d,我可以將 A 映射到與 theta 和 phi 相同數量的行和列。現在,我可以迭代索引并猜測/舍入數組中最匹配的索引B = np.zeros(A.shape)for i in range(0,A.shape[0]): for j in range(0,A.shape[1]): B[i,j] = A[int(f_theta*theta[i,j]),int(f_phi*phi[i,j])]其中f_theta和f_phi是由索引步長測量的步長確定的前因數。這對我來說看起來非常糟糕和低效的編碼,就像我實際想要做的事情的粗略近似(這是反向插值映射?)。這讓我想起了查找表,坐標轉換和插值,但是由于沒有這些關鍵字,我找到了合適的方法來解決問題。我的python經驗大聲疾呼,將有一個模塊/函數,我不知道。編輯限制:A(theta,phi)中的軸的范圍大于θ(x,y)和phi(x,y)的范圍,使得映射值始終存在。我不需要將B映射回A,因此不存在缺失值的問題。映射 A(theta,phi)中的許多值永遠不會被使用。關于清晰度的編輯:我將給出一個帶有小矩陣的示例,希望澄清一些事情:# phi given in degreesphi = np.array([ [2,1,2], [1,0,1], [2,1,2],])# theta given in degreestheta = np.array([ [6,4,6], [4,0,5], [6,5,6],])# A[0,0] is the value at phi=0deg, theta=0deg# A[0,1] is the value at phi=1deg, theta=0deg# A[1,1] is the value at phi=1deg, theta=1deg etc# this is a toy example, the actual A cannot be constructed by a simple ruleA = np.array([ [0.0,0.1,0.2], [1.0,1.1,1.2], [2.0,2.1,2.2], [3.0,3.1,3.2], [4.0,4.1,4.2], [5.0,5.1,5.2], [6.0,6.1,6.2],])# what I want to reach:B = [[6.2,4.1,6.2], [4.1,0.0,5.1], [6.2,5.1,6.2]]我需要澄清的是,我在這里做了一些簡化:1)對于給定的θ,我可以通過查看表格來檢查相應的phi:theta[i,j]對應于phi[i,j]。但是這個例子的構造太簡單了,它們不共享相同的來源,它是嘈雜的數據,因此我無法給出解析表達式theta(phi)或phi(theta)2)我的實際θ和phi中的值是浮點數,我的實際A也以非整數步長測量(例如,在θ方向上每步0.45度,在phi方向上每步0.2度)3)原則上,由于θ和phi之間存在嚴格的關系,我只需要值A的特定1D“跡線”來找到B,但我不知道如何找到這個跡線,也不知道如何創建B出來的跡線。示例中的此跡線為 [A[0,0],A[4,1],A[5,1],A[6,2]] = [0.0,4.1,5.1,6.2]
1 回答

MMMHUHU
TA貢獻1834條經驗 獲得超8個贊
例如,您可以執行雙線性插值:
from scipy.interpolate import interpn
delta = [1.0, 1.0] # theta, phi
points = [np.arange(s)*d for s, d in zip(A.shape, delta)]
xi = np.stack((theta, phi), axis = -1)
B = interpn(points, A, xi)
這給出了:
print(B)
[[6.2 4.1 6.2]
[4.1 0. 5.1]
[6.2 5.1 6.2]]
添加回答
舉報
0/150
提交
取消