In a future release, interp2 will not accept
mixed combinations of row and column vectors for the sample and query
grids. For more information, and recommendations for updating your
code, see Functionality being removed or changed.

Vq = interp2(X,Y,V,Xq,Yq) returns
interpolated values of a function of two variables at specific query
points using linear interpolation. The results always pass through
the original sampling of the function. X and Y contain
the coordinates of the sample points. V contains
the corresponding function values at each sample point. Xq and Yq contain
the coordinates of the query points.

Vq = interp2(V,Xq,Yq) assumes
a default grid of sample points. The default grid points cover the
rectangular region, X=1:n and Y=1:m,
where [m,n] = size(V). Use this syntax to when
you want to conserve memory and are not concerned about the absolute
distances between points.

Vq = interp2(V) returns
the interpolated values on a refined grid formed by dividing the interval
between sample values once in each dimension.

Vq = interp2(___,method) specifies
an optional, trailing input argument that you can pass with any of
the previous syntaxes. The method argument can
be any of the following strings that specify alternative interpolation
methods: 'linear', 'nearest', 'cubic',
or 'spline'. The default method is 'linear'.

Vq = interp2(___,method,extrapval) also
specifies extrapval, a scalar value that is assigned
to all queries that lie outside the domain of the sample points. If
you omit the extrapval argument, then interp2 returns NaN values
for queries outside the domain of the sample points.

Sample grid points, specified as real matrices or vectors.

If X and Y are
matrices, then they contain the coordinates of a full grid (in meshgrid format).
Use the meshgrid function to
create the X and Y matrices
together. Both matrices must be the same size.

If X and Y are
vectors, then they are treated as a grid vectors. The values in both vectors
must be strictly
monotonic and increasing.

Refinement factor, specified as a real, nonnegative, integer
scalar. This value specifies the number of times to repeatedly divide
the intervals of the refined grid in each dimension. This results
in 2^k-1 interpolated points between sample values.

If k is 0, then Vq is
the same as V.

interp2(V,1) is the same as interp2(V).

The following illustration shows the placement of interpolated
values (in red) among nine sample values (in black) for k=2.

Interpolation method, specified as a string from this table.

Method

Description

Continuity

Comments

'linear'

The interpolated value at a query point is based on linear
interpolation of the values at neighboring grid points in each respective
dimension. This is the default interpolation method.

C^{0}

Requires at least two grid points in each dimension

Requires more memory than 'nearest'

'nearest'

The interpolated value at a query point is the value at the
nearest sample grid point.

Discontinuous

Requires two grid points in each dimension.

Fastest computation with modest memory requirements

'cubic'

The interpolated value at a query point is based on a cubic
interpolation of the values at neighboring grid points in each respective
dimension. The interpolation is based on a cubic convolution.

C^{1}

Grid must have uniform spacing in each dimension,
but the spacing does not have to be the same for all dimensions

Requires at least four points in each dimension

Requires more memory and computation time than 'linear'

'spline'

The interpolated value at a query point is based on a cubic
interpolation of the values at neighboring grid points in each respective
dimension. The interpolation is based on a cubic spline using not-a-knot
end conditions.

C^{2}

Requires four points in each dimension

Requires more memory and computation time than 'cubic'

Function value outside domain of X and Y,
specified as a real or complex scalar. interp2 returns
this constant value for all points outside the domain of X and Y.

Example: 5

Example: 5+1i

Data Types: single | double Complex Number Support: Yes

Interpolated values, returned as a real or complex scalar, vector,
or matrix. The size and shape of Vq depends on
the syntax you use and, in some cases, the size and value of the input
arguments.

Syntaxes

Special
Conditions

Size of Vq

Example

interp2(X,Y,V,Xq,Yq) interp2(V,Xq,Yq) and variations of these syntaxes that include method or extrapval

Xq, Yq are scalars

Scalar

size(Vq) = [1 1] when you pass Xq and Yq as
scalars.

Same as above

Xq, Yq are vectors of
the same size and orientation

Vector of same size and orientation as Xq and Yq

If size(Xq) = [100 1] and size(Yq)
= [100 1], then size(Vq) = [100
1].

Same as above

Xq, Yq are vectors of
mixed orientation

Matrix in which the number of rows is length(Yq),
and the number of columns is length(Xq)

If size(Xq) = [1 100] and size(Yq)
= [50 1], then size(Vq) = [50 100].

Same as above

Xq, Yq are matrices or
arrays of the same size

Matrix or array of the same size as Xq and Yq

If size(Xq) = [50 25] and size(Yq)
= [50 25], then size(Vq) = [50
25].

interp2(V,k) and variations
of this syntax that include method or extrapval

None

Matrix in which the number of rows is: 2^k
* (size(V,1)-1)+1,

and the
number of columns is: 2^k * (size(V,2)-1)+1

If size(V) = [10 20] and k
= 2, then size(Vq) = [37 77].

A set of values that are always increasing
or decreasing, without reversals. For example, the sequence, a
= [2 4 6 8] is strictly monotonic and increasing. The sequence, b
= [2 4 4 6 8] is not strictly monotonic because there is
no change in value between b(2) and b(3).
The sequence, c = [2 4 6 8 6] contains a reversal
between c(4) and c(5), so it
is not monotonic at all.

For interp2, the full
grid is a pair of matrices whose elements represent a grid of points
over a rectangular region. One matrix contains the x-coordinates,
and the other matrix contains the y-coordinates.
The values in the x-matrix are strictly monotonic and increasing
along the rows. The values along its columns are constant. The values
in the y-matrix are strictly monotonic and increasing
along the columns. The values along its rows are constant. Use the meshgrid function to create a full grid
that you can pass to interp2.

For example, the following code creates a full grid for the
region, –1 ≤ x ≤ 3 and 1 ≤ y ≤
4:

For interp2, grid vectors
consist of a pair of mixed-orientation vectors that define the x-
and y-coordinates in a grid. One vector is a row
vector, and the other is a column vector.

For example, the following code creates the grid vectors that
specify the region, –1 ≤ x ≤
3 and 1 ≤ y ≤ 4:

For interp2, scattered
points consist of a pair of arrays that define a collection of points
scattered in 2-D space. One array contains the x-coordinates,
and the other contains the y-coordinates.

For example, the following code specifies the points, (2,7),
(5,3), (4,1), and (10,9):