Interpolate 2-D or 3-D scattered data
a surface of the form v = f(x,y) to
the scattered data in the vectors
vq = griddata(
interpolates the surface at the query points specified by
returns the interpolated values,
vq. The surface
always passes through the data points defined by
Interpolate Scattered Data Over Uniform Grid
Interpolate randomly scattered data on a uniform grid of query points.
Sample a function at 200 random points between
rng('default') xy = -2.5 + 5*rand([200 2]); x = xy(:,1); y = xy(:,2); v = x.*exp(-x.^2-y.^2);
v are vectors containing scattered (nonuniform) sample points and data.
Define a regular grid and interpolate the scattered data over the grid.
[xq,yq] = meshgrid(-2:.2:2, -2:.2:2); vq = griddata(x,y,v,xq,yq);
Plot the gridded data as a mesh and the scattered data as dots.
mesh(xq,yq,vq) hold on plot3(x,y,v,'o') xlim([-2.7 2.7]) ylim([-2.7 2.7])
Interpolate 4-D Data Set Over Grid
Interpolate a 3-D slice of a 4-D function that is sampled at randomly scattered points.
Sample a 4-D function at 2500 random points between
1. The vectors
z contain the nonuniform sample points.
x = 2*rand(2500,1) - 1; y = 2*rand(2500,1) - 1; z = 2*rand(2500,1) - 1; v = x.^2 + y.^3 - z.^4;
Define a regular grid with xy points in the range [-1, 1], and set . Interpolating on this grid of 2-D query points
(xq,yq,0) produces a 3-D interpolated slice
(xq,yq,0,vq) of the 4-D data set
d = -1:0.05:1; [xq,yq,zq] = meshgrid(d,d,0);
Interpolate the scattered data on the grid. Plot the results.
vq = griddata(x,y,z,v,xq,yq,zq); plot3(x,y,v,'ro') hold on surf(xq,yq,vq)
Comparison of Scattered Data Interpolation Methods
Compare the results of several different interpolation algorithms offered by
Create a sample data set of 50 scattered points. The number of points is artificially small to highlight the differences between the interpolation methods.
x = -3 + 6*rand(50,1); y = -3 + 6*rand(50,1); v = sin(x).^4 .* cos(y);
Create a grid of query points.
[xq,yq] = meshgrid(-3:0.1:3);
Interpolate the sample data using the
'cubic' methods. Plot the results for comparison.
z1 = griddata(x,y,v,xq,yq,'nearest'); plot3(x,y,v,'mo') hold on mesh(xq,yq,z1) title('Nearest Neighbor') legend('Sample Points','Interpolated Surface','Location','NorthWest')
z2 = griddata(x,y,v,xq,yq,'linear'); figure plot3(x,y,v,'mo') hold on mesh(xq,yq,z2) title('Linear') legend('Sample Points','Interpolated Surface','Location','NorthWest')
z3 = griddata(x,y,v,xq,yq,'natural'); figure plot3(x,y,v,'mo') hold on mesh(xq,yq,z3) title('Natural Neighbor') legend('Sample Points','Interpolated Surface','Location','NorthWest')
z4 = griddata(x,y,v,xq,yq,'cubic'); figure plot3(x,y,v,'mo') hold on mesh(xq,yq,z4) title('Cubic') legend('Sample Points','Interpolated Surface','Location','NorthWest')
Plot the exact solution.
figure plot3(x,y,v,'mo') hold on mesh(xq,yq,sin(xq).^4 .* cos(yq)) title('Exact Solution') legend('Sample Points','Exact Surface','Location','NorthWest')
z — Sample point coordinates
Sample point coordinates, specified as vectors. Corresponding
the xyz coordinates of points where the sample
v are known. The sample points must be unique.
v — Sample values
Sample values, specified as a vector. The sample values in
to the sample points in
v contains complex numbers, then
the real and imaginary parts separately.
Complex Number Support: Yes
zq — Query points
vector | array
Query points, specified as vectors or arrays. Corresponding
elements in the vectors or arrays specify the xyz coordinates
of the query points. The query points are the locations where
The specified query points must lie inside the convex hull of
the sample data points.
query points outside of the convex hull.
method — Interpolation method
'linear' (default) |
Interpolation method, specified as one of the methods in this table.
|Triangulation-based linear interpolation (default) supporting 2-D and 3-D interpolation.||C0|
|Triangulation-based nearest neighbor interpolation supporting 2-D and 3-D interpolation.||Discontinuous|
|Triangulation-based natural neighbor interpolation supporting 2-D and 3-D interpolation. This method is an efficient tradeoff between linear and cubic.||C1 except at sample points|
|Triangulation-based cubic interpolation supporting 2-D interpolation only.||C2|
Biharmonic spline interpolation (MATLAB® 4
vq — Interpolated values
vector | array
Interpolated values, returned as a vector or array. The size
vq depends on the size of the query point inputs
For 2-D interpolation, where
ngrid of query points,
For 3-D interpolation, where
pgrid of query points,
zqfor 3-D interpolation) are vectors that specify scattered points, then
vqis a vector of the same length.
For all interpolation methods other than
'v4', the output
NaN values for query
points outside the convex hull of the sample data. The
'v4' method performs the same calculation for all
points regardless of location.
Yq — Grid coordinates for query points
vectors | matrices
Grid coordinates for query points, returned as vectors or matrices. The
Yq depends on how you
If you specify
xqas a row vector and
yqas a column vector, then
griddatauses those grid vectors to form a full grid with
[Xq,Yq] = meshgrid(xq,yq). In this case, the
Yqoutputs are returned as matrices that contain the full grid coordinates for the query points.
yqare both row vectors or both column vectors, then
Xq = xqand
Yq = yq.
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.