Perform n-dimensional interpolation, where n is at least two. Each element of then n-dimensional array v represents a value at a location given by the parameters x1, x2, ..., xn. The parameters x1, x2, ..., xn are either n-dimensional arrays of the same size as the array v in the 'ndgrid' format or vectors. The parameters y1, etc respect a similar format to x1, etc, and they represent the points at which the array vi is interpolated.
If x1, ..., xn are omitted, they are assumed to be
x1 = 1 : size (
v, 1)
, etc. If m is specified, then the interpolation adds a point half way between each of the interpolation points. This process is performed m times. If only v is specified, then m is assumed to be1
.Method is one of:
- 'nearest'
- Return the nearest neighbour.
- 'linear'
- Linear interpolation from nearest neighbours.
- 'cubic'
- Cubic interpolation from four nearest neighbours (not implemented yet).
- 'spline'
- Cubic spline interpolation–smooth first and second derivatives throughout the curve.
The default method is 'linear'.
If extrapval is the scalar value, use it to replace the values beyond the endpoints with that number. If extrapval is missing, assume NA.
The following code
A=[13,-1,12;5,4,3;1,6,2]; x=[0,1,2]; y=[10,11,12]; xi=linspace(min(x),max(x),17); yi=linspace(min(y),max(y),26)'; mesh(xi,yi,interpn(x,y,A.',xi,yi,"spline").'); [x,y] = meshgrid(x,y); hold on; plot3(x(:),y(:),A(:),"b*"); hold off;
Produces the following figure
![]() |
The following code
x = y = z = -1:1; f = @(x,y,z) x.^2 - y - z.^2; [xx, yy, zz] = meshgrid (x, y, z); v = f (xx,yy,zz); xi = yi = zi = -1:0.1:1; [xxi, yyi, zzi] = ndgrid (xi, yi, zi); vi = interpn(x, y, z, v, xxi, yyi, zzi, 'spline'); mesh (yi, zi, squeeze (vi(1,:,:)));
Produces the following figure
![]() |