[英]How to perform interpolation on a 2D array in MATLAB
如何創建一個包含 2 個變量的函數並給定一個 2D 數組,它會返回一個內插值?
我有N x M
數組A
。 我需要對它進行插值並以某種方式獲得該表面的函數,以便我可以選擇非整數參數的值。 (我需要將該插值用作 2 個變量的函數)
例如:
A[N,M] //my array
// here is the method I'm looking for. Returns function interpolatedA
interpolatedA(3.14,344.1) //That function returns interpolated value
下面是一個使用scatteredInterpolant
的例子:
%# get some 2D matrix, and plot as surface
A = peaks(15);
subplot(121), surf(A)
%# create interpolant
[X,Y] = meshgrid(1:size(A,2), 1:size(A,1));
F = scatteredInterpolant(X(:), Y(:), A(:), 'linear');
%# interpolate over a finer grid
[U,V] = meshgrid(linspace(1,size(A,2),50), linspace(1,size(A,1),50));
subplot(122), surf(U,V, F(U,V))
請注意,您可以隨時評估插值對象:
>> F(3.14,3.41)
ans =
0.036288
上面的示例使用矢量化調用在網格的所有點進行插值
你見過interp2
函數嗎?
從 MatLab 文檔:
ZI = interp2(X,Y,Z,XI,YI)
返回矩陣ZI
其中包含與XI
和YI
元素對應的元素,並由矩陣X
、 Y
和Z
指定的二維函數內的插值確定。 X
和Y
必須是單調的,並且具有相同的格式(“格子”),就好像它們是由meshgrid
生成的meshgrid
。 矩陣X
和Y
指定給出數據Z
的點。 超出范圍的值作為NaN
返回。
像這樣使用spline()
命令:
% A contains N rows and 2 columns
pp = spline(A(:,1), A(:,2));
ppval(pp,3.44)
ans =
0.4454
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.