| MATLAB Function Reference | ![]() |
Syntax
ZI = interp2(X,Y,Z,XI,YI)
ZI = interp2(Z,XI,YI)
ZI = interp2(Z,ntimes)
ZI = interp2(X,Y,Z,XI,YI,method)
Description
ZI = interp2(X,Y,Z,XI,YI)
傳回一個矩陣 ZI ,其包含的元素會對應到由 X 、 Y 和 Z 所產生二維內插的值,XI 和 YI 則是內插點的x及y座標。 X 和 Y 必須是單調的(monotonic),且與 ("plaid") 有相同的格式,就好像它們是由 meshgrid 所產生的。在範圍之外的值會傳回 NaNs。
XI 和 YI 可以是矩陣,而這種情況下 interp2 會傳回對應到 (XI(i,j),YI(i,j)) 的 Z 值。此外,還可以分別傳進 xi 和 yi 的行向量與列向量的值,就好像是用 meshgrid(xi,yi)。
ZI = interp2(Z,XI,YI)
會假設 X = 1:n 且 Y = 1:m, [m,n] = size(Z)。
ZI = interp2(Z,ntimes)
藉著在每個元素間插入內插值來延展 Z ,遞迴的作 ntimes 次。 interp2(Z) 就等於 interp2(Z,1)。
ZI = interp2(X,Y,Z,XI,YI, 可以指定內插的方法:method)
這裡所有的內插方法都需要 X 和 Y 是單調的(monotonic),且與 ("plaid") 有相同的格式就好像由 meshgrid 所產生的。在進行內插之前,會把 X 、 Y 、 XI 和 YI 調整成等距來作處理。當 X 與 Y 已是等距且單調時,就可以用 '*linear'、 '*cubic' 、 '*spline' 或 '*nearest' 來加快執行的速度。
Remarks
內插相當於查表的運作 (table lookup)。在對照表中,table 為 tab = [NaN,Y; X,Z] , interp2 會根據位置在 X 中查詢 XI , Y 中查詢 YI ,傳回 Z 中內插後的元素 ZI 。
Examples
在 finer grid 上對 peaks 函式所產生的資料點作內插 :
[X,Y] = meshgrid(-3:.25:3); Z = peaks(X,Y); [XI,YI] = meshgrid(-3:.125:3); ZI = interp2(X,Y,Z,XI,YI); mesh(X,Y,Z), hold, mesh(XI,YI,ZI+15) hold off axis([-3 3 -3 3 -5 20])
![]()
years = 1950:10:1990; service = 10:10:30; wage = [150.697 199.592 187.625 179.323 195.072 250.287 203.212 179.092 322.767 226.505 153.706 426.730 249.633 120.281 598.243];
可以用內插法求出已服務15年的員工,在 1975 年所賺的薪水:
w = interp2(service,years,wage,15,1975) w = 190.6287
See Also
griddata, interp1, interp3, interpn, meshgrid
| interp1 | interp3 | ![]() |