Interpolation for 1-D, 2-D, 3-D, and N-D gridded data in ndgrid format
interpolated values of a function of n variables
at specific query points using linear interpolation. The results always
pass through the original sampling of the function.
Vq = interpn(
the coordinates of the sample points.
the corresponding function values at each sample point.
the coordinates of the query points.
assumes a default grid of sample points. The default grid consists of the
points, 1,2,3,...ni in each dimension. The value of
ni is the length of the ith dimension in
Vq = interpn(
V. Use this syntax when you want to conserve memory and
are not concerned about the absolute distances between points.
Vq = interpn(___,
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 for queries
outside the domain of the sample points, then based on the
one of the following:
The extrapolated values for the
NaNvalues for other interpolation methods
Define the sample points and values.
x = [1 2 3 4 5]; v = [12 16 31 10 6];
Define the query points,
xq, and interpolate.
xq = (1:0.1:5); vq = interpn(x,v,xq,'cubic');
Plot the result.
figure plot(x,v,'o',xq,vq,'-'); legend('Samples','Cubic Interpolation');
Create a set of grid points and corresponding sample values.
[X1,X2] = ndgrid((-5:1:5)); R = sqrt(X1.^2 + X2.^2)+ eps; V = sin(R)./(R);
Interpolate over a finer grid using
Vq = interpn(V,'cubic'); mesh(Vq);
Evaluate Outside Domain of 3-D Function
Create the grid vectors,
x3. These vectors define the points associated with the values in
x1 = 1:100; x2 = (1:50)'; x3 = 1:30;
Define the sample values to be a 100-by-50-by-30 random number array,
V. Use the
gallery function to create the array.
rng('default') V = rand(100,50,30);
V at three points outside the domain of
extrapval = -1.
xq1 = [0 0 0]; xq2 = [0 0 51]; xq3 = [0 101 102]; vq = interpn(x1,x2,x3,V,xq1,xq2,xq3,'linear',-1)
vq = 1×3 -1 -1 -1
All three points evaluate to
-1 because they are outside the domain of
Define an anonymous function that represents .
f = @(x,y,z,t) t.*exp(-x.^2 - y.^2 - z.^2);
Create a grid of points in . Then, pass the points through the function to create the sample values,
[x,y,z,t] = ndgrid(-1:0.2:1,-1:0.2:1,-1:0.2:1,0:2:10); V = f(x,y,z,t);
Now, create the query grid.
[xq,yq,zq,tq] = ... ndgrid(-1:0.05:1,-1:0.08:1,-1:0.05:1,0:0.5:10);
V at the query points.
Vq = interpn(x,y,z,t,V,xq,yq,zq,tq);
Create a movie to show the results.
figure('renderer','zbuffer'); nframes = size(tq, 4); for j = 1:nframes slice(yq(:,:,:,j),xq(:,:,:,j),zq(:,:,:,j),... Vq(:,:,:,j),0,0,0); clim([0 10]); M(j) = getframe; end movie(M);
X1,X2,...,Xn — Sample grid points
arrays | vectors
Sample grid points, specified as real arrays or vectors. The sample grid points must be unique.
X1,X2,...,Xnare arrays, then they contain the coordinates of a full grid (in ndgrid format). Use the
ndgridfunction to create the
X1,X2,...,Xnarrays together. These arrays must be the same size.
X1,X2,...,Xnare vectors, then they are treated as grid vectors. The values in these vectors must be strictly monotonic, either increasing or decreasing.
[X1,X2,X3,X4] = ndgrid(1:30,-10:10,1:5,10:13)
V — Sample values
Sample values, specified as a real or complex array. The size
V depend on the size of
X1,X2,...,Xnare arrays representing a full grid (in
ndgridformat), then the size of
Vmatches the size of any array,
X1,X2,...,Xnare grid vectors, then
Vis an array whose
ith dimension is the same length as grid vector
V contains complex numbers, then
the real and imaginary parts separately.
Complex Number Support: Yes
Xq1,Xq2,...,Xqn — Query points
scalars | vectors | arrays
Query points, specified as a real scalars, vectors, or arrays.
Xq1,Xq2,...,Xqnare scalars, then they are the coordinates of a single query point in Rn.
Xq1,Xq2,...,Xqnare vectors of different orientations, then
Xq1,Xq2,...,Xqnare treated as grid vectors in Rn.
Xq1,Xq2,...,Xqnare vectors of the same size and orientation, then
Xq1,Xq2,...,Xqnare treated as scattered points in Rn.
Xq1,Xq2,...,Xqnare arrays of the same size, then they represent either a full grid of query points (in
ndgridformat) or scattered points in Rn.
[X1,X2,X3,X4] = ndgrid(1:10,1:5,7:9,10:11)
k — Refinement factor
1 (default) | real, nonnegative, integer scalar
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
2^k-1 interpolated points between sample values.
the same as
interpn(V,1) is the same as
The following illustration depicts
k=2 in R2.
There are 72 interpolated values in red and 9 sample values in black.
method — Interpolation method
'linear' (default) |
Interpolation method, specified as one of the options in this table.
|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.||C0||
|The interpolated value at a query point is the value at the nearest sample grid point.||Discontinuous||
|Shape-preserving piecewise cubic interpolation (for 1-D only). The interpolated value at a query point is based on a shape-preserving piecewise cubic interpolation of the values at neighboring grid points.||C1||
|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.||C1||
|Modified Akima cubic Hermite interpolation. The interpolated value at a query point is based on a piecewise function of polynomials with degree at most three evaluated using the values of neighboring grid points in each respective dimension. The Akima formula is modified to avoid overshoots.||C1||
|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.||C2||
extrapval — Function value outside domain of
Function value outside domain of
specified as a real or complex scalar.
this constant value for all points outside the domain of
Complex Number Support: Yes
Vq — Interpolated values
scalar | vector | array
Interpolated values, returned as a real or complex scalar, vector,
or array. The size and shape of
Vq depends on the
syntax you use and, in some cases, the size and value of the input
|Syntaxes||Special Conditions||Size of Vq||Example|
and variations of these syntaxes that include
|Same as above||Vector of same size and orientation as ||In 3-D, if |
|Same as above||In 3-D, if |
|Same as above||Array of the same size as ||In 3-D, if |
and variations of this syntax that include
Array in which the length of the
|In 3-D, if |
A set of values that are always increasing
or decreasing, without reversals. For example, the sequence,
= [2 4 6 8] is strictly monotonic and increasing. The sequence,
= [2 4 4 6 8] is not strictly monotonic because there is
no change in value between
c = [2 4 6 8 6] contains a reversal
c(5), so it
is not monotonic at all.
Full Grid (in ndgrid Format)
interpn, the full
grid consists of n arrays,
whose elements represent a grid of points in Rn.
The ith array,
Xi, contains strictly monotonic,
increasing values that vary most rapidly along the ith dimension.
to create a full grid that you can pass to
For example, the following code creates a full grid in R2 for
the region, 1 ≤ X1 ≤ 3, 1≤ X2 ≤
[X1,X2] = ndgrid(-1:3,(1:4))
X1 = -1 -1 -1 -1 0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3 X2 = 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
interpn, grid vectors
consist of n vectors of mixed-orientation that
define the points of a grid in Rn.
For example, the following code creates the grid vectors in R3 for the region, 1 ≤ x1 ≤ 3, 4 ≤ x2 ≤ 5, and 6 ≤x3≤ 8:
x1 = 1:3; x2 = (4:5)'; x3 = 6:8;
points consist of n arrays or vectors,
that define a collection of points scattered in Rn.
the coordinates in the
For example, the following code specifies the points, (1, 19, 10), (6, 40, 1), (15, 33, 22), and (0, 61, 13) in R3.
Xq1 = [1 6; 15 0]; Xq2 = [19 40; 33 61]; Xq3 = [10 1; 22 13];
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
For best results, provide
X1,X2,...,Xnas vectors. The values in these vectors must be strictly monotonic and increasing.
Code generation does not support the
The interpolation method must be a constant character vector.
Run code in the background using MATLAB®
backgroundPool or accelerate code with Parallel Computing Toolbox™
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
Usage notes and limitations:
A maximum of five dimensions is supported.
Vmust be a double or single N-D array.
Vcan be real or complex.
Have the same type (double or single).
Be finite vectors or N-D arrays with increasing and nonrepeating elements in corresponding dimensions.
Align with Cartesian axes when
X1,X2,...,Xnare N-D arrays (as if they were produced by
Have dimensions consistent with
X1,X2,...,Xnmust be vectors or arrays of the same type (double or single). If
X1,X2,...,Xnare arrays, then they must have the same size. If they are vectors with different lengths, then one of them must have a different orientation.
The extrapolation for the out-of-boundary input is not supported.
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Introduced before R2006a