Navigation

Operators and Keywords

Function List:

C++ API

: zi = interp2 (x, y, z, xi, yi)
: zi = interp2 (z, xi, yi)
: zi = interp2 (z, n)
: zi = interp2 (z)
: zi = interp2 (…, method)
: zi = interp2 (…, method, extrap)

Two-dimensional interpolation.

Interpolate reference data x, y, z to determine zi at the coordinates xi, yi. The reference data x, y can be matrices, as returned by meshgrid, in which case the sizes of x, y, and z must be equal. If x, y are vectors describing a grid then length (x) == columns (z) and length (y) == rows (z). In either case the input data must be strictly monotonic.

If called without x, y, and just a single reference data matrix z, the 2-D region x = 1:columns (z), y = 1:rows (z) is assumed. This saves memory if the grid is regular and the distance between points is not important.

If called with a single reference data matrix z and a refinement value n, then perform interpolation over a grid where each original interval has been recursively subdivided n times. This results in 2^n-1 additional points for every interval in the original grid. If n is omitted a value of 1 is used. As an example, the interval [0,1] with n==2 results in a refined interval with points at [0, 1/4, 1/2, 3/4, 1].

The interpolation method is one of:

"nearest"

Return the nearest neighbor.

"linear" (default)

Linear interpolation from nearest neighbors.

"pchip"

Piecewise cubic Hermite interpolating polynomial—shape-preserving interpolation with smooth first derivative.

"cubic"

Cubic interpolation (same as "pchip").

"spline"

Cubic spline interpolation—smooth first and second derivatives throughout the curve.

extrap is a scalar number. It replaces values beyond the endpoints with extrap. Note that if extrapval is used, method must be specified as well. If extrap is omitted and the method is "spline", then the extrapolated values of the "spline" are used. Otherwise the default extrap value for any other method is "NA".

See also: interp1, interp3, interpn, meshgrid.

Demonstration 1

The following code

 clf;
 colormap ("default");
 A = [13,-1,12;5,4,3;1,6,2];
 x = [0,1,4];  y = [10,11,12];
 xi = linspace (min (x), max (x), 17);
 yi = linspace (min (y), max (y), 26)';
 mesh (xi,yi,interp2 (x,y,A,xi,yi, "linear"));
 [x,y] = meshgrid (x,y);
 hold on; plot3 (x,y,A,"b*"); hold off;

Produces the following figure

Figure 1

Demonstration 2

The following code

 clf;
 colormap ("default");
 [x,y,A] = peaks (10);
 x = x(1,:)';  y = y(:,1);
 xi = linspace (min (x), max (x), 41);
 yi = linspace (min (y), max (y), 41)';
 mesh (xi,yi,interp2 (x,y,A,xi,yi, "linear"));
 [x,y] = meshgrid (x,y);
 hold on; plot3 (x,y,A,"b*"); hold off;

Produces the following figure

Figure 1

Demonstration 3

The following code

 clf;
 colormap ("default");
 A = [13,-1,12;5,4,3;1,6,2];
 x = [0,1,4];  y = [10,11,12];
 xi = linspace (min (x), max (x), 17);
 yi = linspace (min (y), max (y), 26)';
 mesh (xi,yi,interp2 (x,y,A,xi,yi, "nearest"));
 [x,y] = meshgrid (x,y);
 hold on; plot3 (x,y,A,"b*"); hold off;

Produces the following figure

Figure 1

Demonstration 4

The following code

 clf;
 colormap ("default");
 [x,y,A] = peaks (10);
 x = x(1,:)';  y = y(:,1);
 xi = linspace (min (x), max (x), 41);
 yi = linspace (min (y), max (y), 41)';
 mesh (xi,yi,interp2 (x,y,A,xi,yi, "nearest"));
 [x,y] = meshgrid (x,y);
 hold on; plot3 (x,y,A,"b*"); hold off;

Produces the following figure

Figure 1

Demonstration 5

The following code

 clf;
 colormap ("default");
 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,interp2 (x,y,A,xi,yi, "cubic"));
 [x,y] = meshgrid (x,y);
 hold on; plot3 (x,y,A,"b*"); hold off;

Produces the following figure

Figure 1

Demonstration 6

The following code

 clf;
 colormap ("default");
 [x,y,A] = peaks (10);
 x = x(1,:)';  y = y(:,1);
 xi = linspace (min (x), max (x), 41);
 yi = linspace (min (y), max (y), 41)';
 mesh (xi,yi,interp2 (x,y,A,xi,yi, "cubic"));
 [x,y] = meshgrid (x,y);
 hold on; plot3 (x,y,A,"b*"); hold off;

Produces the following figure

Figure 1

Demonstration 7

The following code

 clf;
 colormap ("default");
 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,interp2 (x,y,A,xi,yi, "spline"));
 [x,y] = meshgrid (x,y);
 hold on; plot3 (x,y,A,"b*"); hold off;

Produces the following figure

Figure 1

Demonstration 8

The following code

 clf;
 colormap ("default");
 [x,y,A] = peaks (10);
 x = x(1,:)';  y = y(:,1);
 xi = linspace (min (x), max (x), 41);
 yi = linspace (min (y), max (y), 41)';
 mesh (xi,yi,interp2 (x,y,A,xi,yi, "spline"));
 [x,y] = meshgrid (x,y);
 hold on; plot3 (x,y,A,"b*"); hold off;

Produces the following figure

Figure 1

Package: octave