This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.


Interpolate 2-D or 3-D scattered data


vq = griddata(x,y,v,xq,yq)
vq = griddata(x,y,z,v,xq,yq,zq)
vq = griddata(___,method)



vq = griddata(x,y,v,xq,yq) fits a surface of the form v = f(x,y) to the scattered data in the vectors (x,y,v). The griddata function interpolates the surface at the query points specified by (xq,yq) and returns the interpolated values, vq. The surface always passes through the data points defined by x and y.


vq = griddata(x,y,z,v,xq,yq,zq) fits a hypersurface of the form v = f(x,y,z).

vq = griddata(___,method) specifies the interpolation method used to compute vq using any of the input arguments in the previous syntaxes.

    Note:   Qhull-specific options are no longer supported. Remove the options argument from all instances in your code that pass it to griddata.

    In a future release, griddata will not accept any input vectors of mixed orientation. In addition, the following syntaxes will be removed:

    [Xq,Yq,Vq] = griddata(x,y,v,xq,yq)
    [Xq,Yq,Vq] = griddata(x,y,v,xq,yq, method)
    To specify a grid of query points, construct a full grid with ndgrid or meshgrid before calling griddata.


collapse all

Interpolate randomly scattered data on a uniform grid of query points.

Sample a function at 200 random points between -2.5 and 2.5.

xy = -2.5 + 5*gallery('uniformdata',[200 2],0);
x = xy(:,1);
y = xy(:,2);
v = x.*exp(-x.^2-y.^2);

x, y, and 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.

hold on
xlim([-2.7 2.7])
ylim([-2.7 2.7])

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 and 1. The vectors x, y, and 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 produces a 3-D interpolated slice 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);
hold on

Compare the results of several different interpolation algorithms offered by griddata.

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 'nearest', 'linear', 'natural', and 'cubic' methods. Plot the results for comparison.

z1 = griddata(x,y,v,xq,yq,'nearest');
hold on
title('Nearest Neighbor')
legend('Sample Points','Interpolated Surface','Location','NorthWest')

z2 = griddata(x,y,v,xq,yq,'linear');
hold on
legend('Sample Points','Interpolated Surface','Location','NorthWest')

z3 = griddata(x,y,v,xq,yq,'natural');
hold on
title('Natural Neighbor')
legend('Sample Points','Interpolated Surface','Location','NorthWest')

z4 = griddata(x,y,v,xq,yq,'cubic');
hold on
legend('Sample Points','Interpolated Surface','Location','NorthWest')

Plot the exact solution.

hold on
mesh(xq,yq,sin(xq).^4 .* cos(yq))
title('Exact Solution')
legend('Sample Points','Exact Surface','Location','NorthWest')

Input Arguments

collapse all

Sample point coordinates, specified as vectors. Corresponding elements in x, y, and z specify the xyz coordinates of points where the sample values v are known. The sample points must be unique.

Data Types: single | double

Sample values, specified as a vector. The sample values in v correspond to the sample points in x, y, and z.

If v contains complex numbers, then griddata interpolates the real and imaginary parts separately.

Data Types: single | double
Complex Number Support: Yes

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 griddata performs interpolation.

  • Specify arrays if you want to pass a grid of query points. Use ndgrid or meshgrid to construct the arrays.

  • Specify vectors if you want to pass a collection of scattered points.

The specified query points must lie inside the convex hull of the sample data points. griddata returns NaN for query points outside of the convex hull.

Data Types: single | double

Interpolation method, specified as one of the methods in this table.

'linear'Triangulation-based linear interpolation (default) supporting 2-D and 3-D interpolation.C0
'nearest'Triangulation-based nearest neighbor interpolation supporting 2-D and 3-D interpolation.Discontinuous
'natural'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
'cubic'Triangulation-based cubic interpolation supporting 2-D interpolation only.C2

Biharmonic spline interpolation (MATLAB® 4 griddata method) supporting 2-D interpolation only. Unlike the other methods, this interpolation is not based on a triangulation.


Data Types: char

Output Arguments

collapse all

Interpolated values, returned as a vector or array. The size of vq depends on the size of the query point inputs xq, yq, and zq:

  • For 2-D interpolation, where xq and yq specify an m-by-n grid of query points, vq is an m-by-n array.

  • For 3-D interpolation, where xq, yq, and zq specify an m-by-n-by-p grid of query points, vq is an m-by-n-by-p array.

  • If xq, yq, (and zq for 3-D interpolation) are vectors that specify scattered points, then vq is a vector of the same length.

vq contains NaN values for query points outside the convex hull of the sample data.

Introduced before R2006a

Was this topic helpful?