Interpolation for 2D gridded data in meshgrid format
collapse all in page
Syntax
Vq = interp2(X,Y,V,Xq,Yq)
Vq = interp2(V,Xq,Yq)
Vq = interp2(V)
Vq = interp2(V,k)
Vq = interp2(___,method)
Vq = interp2(___,method,extrapval)
Description
example
Vq = interp2(X,Y,V,Xq,Yq)
returnsinterpolated values of a function of two variables at specific querypoints using linear interpolation. The results always pass throughthe original sampling of the function. X
and Y
containthe coordinates of the sample points. V
containsthe corresponding function values at each sample point. Xq
and Yq
containthe 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 when you want to conserve memory and are not concerned about the absolute distances between points.
Vq = interp2(V)
returnsthe interpolated values on a refined grid formed by dividing the intervalbetween sample values once in each dimension.
example
Vq = interp2(V,k)
returns the interpolated values on a refined grid formed by repeatedlyhalving the intervals k
times in each dimension.This results in 2^k1
interpolated points betweensample values.
example
Vq = interp2(___,method)
specifies an alternative interpolation method: 'linear'
, 'nearest'
, 'cubic'
, 'makima'
, or 'spline'
. The default method is 'linear'
.
example
Vq = interp2(___,method,extrapval)
alsospecifies extrapval
, a scalar value that is assignedto all queries that lie outside the domain of the sample points.
If you omit the extrapval
argument for queriesoutside the domain of the sample points, then based on the method
argument interp2
returnsone of the following:
Extrapolated values for the
'spline'
and'makima'
methodsNaN
values for other interpolation methods
Examples
collapse all
Interpolate over a Grid Using Default Method
Open Live Script
Coarsely sample the peaks
function.
[X,Y] = meshgrid(3:3);V = peaks(X,Y);
Plot the coarse sampling.
figuresurf(X,Y,V)title('Original Sampling');
Create the query grid with spacing of 0.25.
[Xq,Yq] = meshgrid(3:0.25:3);
Interpolate at the query points.
Vq = interp2(X,Y,V,Xq,Yq);
Plot the result.
figuresurf(Xq,Yq,Vq);title('Linear Interpolation Using Finer Grid');
Interpolate over a Grid Using Cubic Method
Open Live Script
Coarsely sample the peaks function.
[X,Y] = meshgrid(3:3);V = peaks(7);
Plot the coarse sampling.
figuresurf(X,Y,V)title('Original Sampling');
Create the query grid with spacing of 0.25.
[Xq,Yq] = meshgrid(3:0.25:3);
Interpolate at the query points, and specify cubic interpolation.
Vq = interp2(X,Y,V,Xq,Yq,'cubic');
Plot the result.
figuresurf(Xq,Yq,Vq);title('Cubic Interpolation Over Finer Grid');
Refine Grayscale Image
Open Live Script
Load some image data into the workspace.
load flujet.matcolormap gray
Isolate a small region of the image and cast it to singleprecision.
V = single(X(200:300,1:25));
Display the image region.
imagesc(V);axis offtitle('Original Image')
Insert interpolated values by repeatedly dividing the intervals between points of the refined grid five times in each dimension.
Vq = interp2(V,5);
Display the result.
imagesc(Vq);axis offtitle('Linear Interpolation')
Evaluate Outside the Domain of X and Y
Open Live Script
Coarsely sample a function over the range, [2, 2]
in both dimensions.
[X,Y] = meshgrid(2:0.75:2);R = sqrt(X.^2 + Y.^2)+ eps;V = sin(R)./(R);
Plot the coarse sampling.
figuresurf(X,Y,V)xlim([4 4])ylim([4 4])title('Original Sampling')
Create the query grid that extends beyond the domain of X
and Y
.
[Xq,Yq] = meshgrid(3:0.2:3);
Perform cubic interpolation within the domain of X
and Y
, and assign all queries that fall outside to zero.
Vq = interp2(X,Y,V,Xq,Yq,'cubic',0);
Plot the result.
figuresurf(Xq,Yq,Vq)title('Cubic Interpolation with Vq=0 Outside Domain of X and Y');
Input Arguments
collapse all
X,Y
— Sample grid points
matrices  vectors
Sample grid points, specified as real matrices or vectors. Thesample grid points must be unique.
If
X
andY
arematrices, then they contain the coordinates of a full grid (in meshgrid format).Use the meshgrid function tocreate theX
andY
matricestogether. Both matrices must be the same size.If
X
andY
are vectors, then they are treated as grid vectors. The values in both vectors must be strictly monotonic, either increasing or decreasing.
Example: [X,Y] = meshgrid(1:30,10:10)
Data Types: single
 double
V
— Sample values
matrix
Sample values, specified as a real or complex matrix. The sizerequirements for V
depend on the size of X
and Y
:
If
X
andY
arematrices representing a full grid (inmeshgrid
format),thenV
must be the same size asX
andY
.If
X
andY
aregrid vectors, thenV
must be a matrix containinglength(Y)
rowsandlength(X)
columns.
If V
contains complex numbers, then interp2
interpolatesthe real and imaginary parts separately.
Example: rand(10,10)
Data Types: single
 double
Complex Number Support: Yes
Xq,Yq
— Query points
scalars  vectors  matrices  arrays
Query points, specified as a real scalars, vectors, matrices,or arrays.
If
Xq
andYq
arescalars, then they are the coordinates of a single query point.If
Xq
andYq
arevectors of different orientations, thenXq
andYq
aretreated as grid vectors.If
Xq
andYq
arevectors of the same size and orientation, thenXq
andYq
aretreated as scatteredpoints in 2D space.If
Xq
andYq
arematrices, then they represent either a full grid of query points (inmeshgrid
format)or scattered points.If
Xq
andYq
areND arrays, then they represent scattered points in 2D space.
Example: [Xq,Yq] = meshgrid((1:0.1:10),(5:0.1:0))
Data Types: single
 double
k
— Refinement factor
1
(default)  real, nonnegative, integer scalar
Refinement factor, specified as a real, nonnegative, integerscalar. This value specifies the number of times to repeatedly dividethe intervals of the refined grid in each dimension. This resultsin 2^k1
interpolated points between sample values.
If k
is 0
, then Vq
isthe same as V
.
interp2(V,1)
is the same as interp2(V)
.
The following illustration shows the placement of interpolatedvalues (in red) among nine sample values (in black) for k=2
.
Example: interp2(V,2)
Data Types: single
 double
method
— Interpolation method
'linear'
(default)  'nearest'
 'cubic'
 'spline'
 'makima'
Interpolation method, specified as one of the options in 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} 

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

'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} 

'makima'  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.  C^{1} 

'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 notaknot end conditions.  C^{2} 

extrapval
— Function value outside domain of X
and Y
scalar
Function value outside domain of X
and Y
,specified as a real or complex scalar. interp2
returnsthis 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
Output Arguments
collapse all
Vq
— Interpolated values
scalar  vector  matrix
Interpolated values, returned as a real or complex scalar, vector,or matrix. The size and shape of Vq
depends onthe syntax you use and, in some cases, the size and value of the inputarguments.
Syntaxes  SpecialConditions  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 asscalars. 
Same as above  Xq , Yq are vectors ofthe 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) = [1001] . 
Same as above  Xq , Yq are vectors ofmixed 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 orarrays 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) = [5025] . 
interp2(V,k) and variationsof this syntax that include method or extrapval  None  Matrix in which the number of rows is: and thenumber of columns is:  If size(V) = [10 20] and k= 2 , then size(Vq) = [37 77] . 
More About
collapse all
Strictly Monotonic
A set of values that are always increasingor 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 isno change in value between b(2)
and b(3)
.The sequence, c = [2 4 6 8 6]
contains a reversalbetween c(4)
and c(5)
, so itis not monotonic at all.
Full Grid (in meshgrid Format)
For interp2
, the fullgrid is a pair of matrices whose elements represent a grid of pointsover a rectangular region. One matrix contains the xcoordinates,and the other matrix contains the ycoordinates.The values in the xmatrix are strictly monotonic and increasingalong the rows. The values along its columns are constant. The valuesin the ymatrix are strictly monotonic and increasingalong the columns. The values along its rows are constant. Use the meshgrid function to create a full gridthat you can pass to interp2
.
For example, the following code creates a full grid for theregion, –1 ≤ x ≤ 3 and 1 ≤ y ≤4:
[X,Y] = meshgrid(1:3,(1:4))
X = 1 0 1 2 3 1 0 1 2 3 1 0 1 2 3 1 0 1 2 3Y = 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4
Grid vectors are a more compact format to represent a grid than the full grid. The relation between the two formats and the matrix of sample values V
is
Grid Vectors
For interp2
, grid vectors consist of a pair of vectors that define the x and ycoordinates in a grid. The row vector defines xcoordinates, and the column vector defines ycoordinates.
For example, the following code creates the grid vectors that specify the region, –1 ≤ x ≤ 3 and 1 ≤ y ≤ 4:
x = 1:3;y = (1:4)';
Scattered Points
For interp2
, scatteredpoints consist of a pair of arrays that define a collection of pointsscattered in 2D space. One array contains the xcoordinates,and the other contains the ycoordinates.
For example, the following code specifies the points, (2,7),(5,3), (4,1), and (10,9):
x = [2 5; 4 10];y = [7 3; 1 9];
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
Xq
andYq
mustbe the same size. Usemeshgrid
to evaluate ona grid.For best results, provide
X
andY
as vectors. The values in these vectors must be strictly monotonic and increasing.Code generation does not support the
'makima'
interpolation method.For the
'cubic'
interpolation method, if the grid does not have uniform spacing, an error results. In this case, use the'spline'
interpolation method.For best results when you use the
'spline'
interpolation method:Use
meshgrid
to create the inputsXq
andYq
.Use a small number of interpolation points relative to the dimensions of
V
. Interpolating over a large set of scattered points can be inefficient.
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
Usage notes and limitations:
Xq
andYq
must be the same size. Usemeshgrid
to evaluate on a grid.For best results, provide
X
andY
as vectors. The values in these vectors must be strictly monotonic and increasing.Code generation does not support the
'makima'
interpolation method.For the
'cubic'
interpolation method, if the grid does not have uniform spacing, an error results. In this case, use the'spline'
interpolation method.For best results when you use the
'spline'
interpolation method:Use
meshgrid
to create the inputsXq
andYq
.Use a small number of interpolation points relative to the dimensions of
V
. Interpolating over a large set of scattered points can be inefficient.
ThreadBased Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports threadbased environments. For more information, see Run MATLAB Functions in ThreadBased Environment.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
Usage notes and limitations:
V
must be a double or single 2D array.V
can be real or complex.V
cannot be a vector.X
andY
must:Have the same type (double or single).
Be finite vectors or 2D arrays with increasing and nonrepeating elements in corresponding dimensions.
Align with Cartesian axes when
X
andY
are nonvector 2D arrays (as if they were produced bymeshgrid
).Have dimensions consistent with
V
.
Xq
andYq
must be vectors or arrays of the same type (double or single). IfXq
andYq
are arrays, then they must have the same size. If they are vectors with different lengths, then they must have different orientations.method
must be'linear'
,'nearest'
, or'cubic'
.The extrapolation for the outofboundary input is not supported.
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Distributed Arrays
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).
Version History
Introduced before R2006a
See Also
griddata  interp1  interp3  interpn  meshgrid  griddedInterpolant  scatteredInterpolant
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Americas
 América Latina (Español)
 Canada (English)
 United States (English)
Europe
 Belgium (English)
 Denmark (English)
 Deutschland (Deutsch)
 España (Español)
 Finland (English)
 France (Français)
 Ireland (English)
 Italia (Italiano)
 Luxembourg (English)
 Netherlands (English)
 Norway (English)
 Österreich (Deutsch)
 Portugal (English)
 Sweden (English)
 Switzerland
 Deutsch
 English
 Français
 United Kingdom (English)
Contact your local office