Interpolate 2-D or 3-D scattered data

collapse all in page

## Syntax

`vq = griddata(x,y,v,xq,yq)`

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

`vq = griddata(___,method)`

`[Xq,Yq,vq] = griddata(x,y,v,xq,yq)`

`[Xq,Yq,vq] = griddata(x,y,v,xq,yq,method)`

## Description

example

`vq = griddata(x,y,v,xq,yq)`

fitsa surface of the form *v* = *f*(*x*,*y*) tothe scattered data in the vectors `(x,y,v)`

. The `griddata`

functioninterpolates the surface at the query points specified by `(xq,yq)`

andreturns the interpolated values, `vq`

. The surfacealways passes through the data points defined by `x`

and `y`

.

example

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

fitsa 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. `method`

can be `"linear"`

, `"nearest"`

, `"natural"`

, `"cubic"`

, or `"v4"`

. The default method is `"linear"`

.

`[Xq,Yq,vq] = griddata(x,y,v,xq,yq)`

and `[`

additionally return `Xq`

,`Yq`

,`vq`

] = griddata(`x`

,`y`

,`v`

,`xq`

,`yq`

,method)`Xq`

and `Yq`

, which contain the grid coordinates for the query points.

## Examples

collapse all

### Interpolate Scattered Data over Uniform Grid

Open Live Script

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

Sample a function at 200 random points between `-2.5`

and `2.5`

. The resulting vectors `x`

, `y`

, and `v`

contain scattered sample points and data values at those points.

`rng defaultxy = -2.5 + 5*rand([200 2]);x = xy(:,1);y = xy(:,2);v = x.*exp(-x.^2-y.^2);`

Define a grid of query points 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 onplot3(x,y,v,"o")xlim([-2.7 2.7])ylim([-2.7 2.7])

### Interpolate 4-D Data Set over Grid

Open Live Script

Interpolate a 3-D slice of a 4-D function that is sampled at randomly scattered points.

Sample a 4-D function $\mathit{v}\left(\mathit{x},\mathit{y},\mathit{z}\right)$ 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;

Create a grid with *xy *points in the range [-1, 1], and set $\mathit{z}=0$. 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 `(x,y,z,v)`

.

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 onsurf(xq,yq,vq)hold off

### Comparison of Scattered Data Interpolation Methods

Open Live Script

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");plot3(x,y,v,"mo")hold onmesh(xq,yq,z1)title("Nearest Neighbor")legend("Sample Points","Interpolated Surface","Location","NorthWest")

z2 = griddata(x,y,v,xq,yq,"linear");figureplot3(x,y,v,"mo")hold onmesh(xq,yq,z2)title("Linear")legend("Sample Points","Interpolated Surface","Location","NorthWest")

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

z4 = griddata(x,y,v,xq,yq,"cubic");figureplot3(x,y,v,"mo")hold onmesh(xq,yq,z4)title("Cubic")legend("Sample Points","Interpolated Surface","Location","NorthWest")

Plot the exact solution.

figureplot3(x,y,v,"mo")hold onmesh(xq,yq,sin(xq).^4 .* cos(yq))title("Exact Solution")legend("Sample Points","Exact Surface","Location","NorthWest")

## Input Arguments

collapse all

`x`

, `y`

, `z`

— Sample point coordinates

vectors

Sample point coordinates, specified as vectors. Correspondingelements in `x`

, `y`

, and `z`

specifythe *xyz* coordinates of points where the samplevalues `v`

are known. The sample points must be unique.

**Data Types: **`double`

`v`

— Sample values

vector

Sample values, specified as a vector. The sample values in `v`

correspondto the sample points in `x`

, `y`

,and `z`

.

If `v`

contains complex numbers, then `griddata`

interpolatesthe real and imaginary parts separately.

**Data Types: **`double`

**Complex Number Support: **Yes

`xq`

, `yq`

, `zq`

— Query points

vector | array

Query points, specified as vectors or arrays. Correspondingelements in the vectors or arrays specify the *xyz* coordinatesof the query points. The query points are the locations where `griddata`

performsinterpolation.

Specify arrays if you want to pass a grid of querypoints. 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 ofthe sample data points. `griddata`

returns `NaN`

forquery points outside of the convex hull.

**Data Types: **`double`

`method`

— Interpolation method

`"linear"`

(default) | `"nearest"`

| `"natural"`

| `"cubic"`

| `"v4"`

Interpolation method, specified as one of the methods in thistable.

Method | Description | Continuity |
---|---|---|

`"linear"` | Triangulation-based linear interpolation (default) supporting2-D and 3-D interpolation. | C^{0} |

`"nearest"` | Triangulation-based nearest neighbor interpolation supporting2-D and 3-D interpolation. | Discontinuous |

`"natural"` | Triangulation-based natural neighbor interpolation supporting2-D and 3-D interpolation. This method is an efficient tradeoff betweenlinear and cubic. | C^{1} except at sample points |

`"cubic"` | Triangulation-based cubic interpolation supporting 2-D interpolationonly. | C^{2} |

`"v4"` | Biharmonic spline interpolation (MATLAB | C^{2} |

**Data Types: **`char`

| `string`

## Output Arguments

collapse all

`vq`

— Interpolated values

vector | array

Interpolated values, returned as a vector or array. The sizeof `vq`

depends on the size of the query point inputs `xq`

, `yq`

,and `zq`

:

For 2-D interpolation, where

`xq`

and`yq`

specifyan`m`

-by-`n`

grid of query points,`vq`

isan`m`

-by-`n`

array.For 3-D interpolation, where

`xq`

,`yq`

,and`zq`

specify an`m`

-by-`n`

-by-`p`

gridof query points,`vq`

is an`m`

-by-`n`

-by-`p`

array.If

`xq`

,`yq`

, (and`zq`

for3-D interpolation) are vectors that specify scattered points, then`vq`

isa vector of the same length.

For all interpolation methods other than `"v4"`

, the output `vq`

contains `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.

`Xq`

, `Yq`

— Grid coordinates for query points

vectors | matrices

Grid coordinates for query points, returned as vectors or matrices. The shape of `Xq`

and `Yq`

depends on how you specify `xq`

and `yq`

:

If you specify

`xq`

as a row vector and`yq`

as a column vector, then`griddata`

uses those grid vectors to form a full grid with`[Xq,Yq] = meshgrid(xq,yq)`

. In this case, the`Xq`

and`Yq`

outputs are returned as matrices that contain the full grid coordinates for the query points.If

`xq`

and`yq`

are both row vectors or both column vectors, then`Xq = xq`

and`Yq = yq`

.

## Tips

Scattered data interpolation with

`griddata`

uses a Delaunay triangulation of the data, so can be sensitive to scaling issues in x, y, and z. When this occurs, you can use normalize to rescale the data and improve the results. See Normalize Data with Differing Magnitudes for more information.

## Extended Capabilities

### Thread-Based Environment

Run code in the background using MATLAB® `backgroundPool`

or accelerate code with Parallel Computing Toolbox™ `ThreadPool`

.

This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.

## Version History

**Introduced before R2006a**

## See Also

scatteredInterpolant | delaunay | griddatan | interpn | meshgrid | ndgrid

## 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