interp2
Interpolation of data on a 2-D grid.
Category: Mathematics > Interpolation
Syntax
Vq = interp2(X, Y, V, Xq, Yq)
Linear interpolation of V sampled on the grid (X,Y) at the points (Xq,Yq).
Vq = interp2(V, Xq, Yq)
Assumes the grid is 1:n by 1:m, the column and row indices of V.
Vq = interp2(..., method, extrapval)
Selects the method ('linear', 'nearest', 'cubic', 'spline', 'makima') and a value for points outside the grid.
Description
interp2 works on data in meshgrid orientation, where the first index of V follows Y and the second follows X. The linear method is bilinear: it interpolates along one axis and then along the other inside each grid cell.
For grids in ndgrid orientation, or for more than two dimensions, use interpn.
Input arguments
X, Y- Grid coordinates as
meshgridarrays or as vectors (X along columns, Y along rows). V- Matrix of values;
V(i,j)belongs to(X(j), Y(i)). Xq, Yq- Query coordinates. Vectors of equal shape are paired point by point.
Output arguments
Vq- Interpolated values. Points outside the grid give
NaNunlessextrapvalis given.
Example
Bilinear and nearest lookups
[X, Y] = meshgrid(1:3);
V = X + 10*Y;
interp2(X, Y, V, 1.5, 2.5)
interp2(V, 1.5, 2.5)
interp2(X, Y, V, 1.5, 2.5, 'nearest')
interp2(X, Y, V, 4, 4, 'linear', 0)
Output
ans = 26.5000
ans = 26.5000
ans = 32
ans = 0