Gridded data interpolation - MATLAB (2024)

Table of Contents
Description Creation Syntax Description Input Arguments x — Sample pointsvector v — Sample values vector | matrix | multidimensional array X1, X2, Xn — Sample points in full grid form arrays gridVecs — Sample points in grid vector form cell array of grid vectors V — Sample values array Method — Interpolation method 'linear' (default) | 'nearest' | 'next' | 'previous' | 'pchip' | 'cubic' | 'spline' | 'makima' ExtrapolationMethod — Extrapolation method 'linear' (default) | 'nearest' | 'next' | 'previous' | 'pchip' | 'cubic' | 'spline' | 'makima' | 'none' Properties GridVectors — Grid vectors cell array Values — Function values at sample points array Method — Interpolation method 'linear' (default) | 'nearest' | 'next' | 'previous' | 'pchip' | 'cubic' | 'spline' | 'makima' ExtrapolationMethod — Extrapolation method 'linear' | 'nearest' | 'next' | 'previous' | 'pchip' | 'cubic' | 'spline' | 'makima' | 'none' Usage Syntax Description Examples 1-D Interpolation 3-D Interpolation Using Full Grid vs. Grid Vectors Interpolation with Default Grid 2-D Interpolation over Finer Grid 1-D Extrapolation Interpolate Multiple Sets of Values on Same Grid More About Interpolant Gridded Data Scattered Data Full Grid Grid Vectors Tips Extended Capabilities C/C++ Code Generation Generate C and C++ code using MATLAB® Coder™. Thread-Based Environment Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool. GPU Arrays Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™. Version History R2021a: Interpolate multiple data sets simultaneously See Also Topics MATLAB Command Americas Europe Asia Pacific

Gridded data interpolation

expand all in page

Description

Use griddedInterpolant to perform interpolation on a 1-D, 2-D, 3-D, or N-D gridded data set. griddedInterpolant returns the interpolant F for the given data set. You can evaluate F at a set of query points, such as (xq,yq) in 2-D, to produce interpolated values vq = F(xq,yq).

Use scatteredInterpolant to perform interpolation with scattered data.

Creation

Syntax

F = griddedInterpolant

F = griddedInterpolant(x,v)

F = griddedInterpolant(X1,X2,...,Xn,V)

F = griddedInterpolant(V)

F = griddedInterpolant(gridVecs,V)

F = griddedInterpolant(___,Method)

F = griddedInterpolant(___,Method,ExtrapolationMethod)

Description

F = griddedInterpolant creates an empty gridded data interpolant object.

example

F = griddedInterpolant(x,v) creates a 1-D interpolant from a vector of sample points x and corresponding values v.

example

F = griddedInterpolant(X1,X2,...,Xn,V) creates a 2-D, 3-D, or N-D interpolant using a full grid of sample points passed as a set of n-dimensional arrays X1,X2,...,Xn. The V array contains the sample values associated with the point locations in X1,X2,...,Xn. Each of the arrays X1,X2,...,Xn must be the same size as V.

example

F = griddedInterpolant(V) uses the default grid to create the interpolant. When you use this syntax, griddedInterpolant defines the grid as a set of points whose spacing is 1 and range is [1, size(V,i)] in the ith dimension. Use this syntax when you want to conserve memory and are not concerned about the absolute distances between points.

example

F = griddedInterpolant(gridVecs,V) specifies a cell array gridVecs that contains n grid vectors to describe an n-dimensional grid of sample points. Use this syntax when you want to use a specific grid and also conserve memory.

example

F = griddedInterpolant(___,Method) specifies the interpolation method: 'linear', 'nearest', 'next', 'previous', 'pchip', 'cubic', 'makima', or 'spline'. You can specify Method as the last input argument in any of the previous syntaxes.

example

F = griddedInterpolant(___,Method,ExtrapolationMethod) specifies both the interpolation and extrapolation methods. griddedInterpolant uses ExtrapolationMethod to estimate the value when your query points fall outside the domain of your sample points.

Input Arguments

expand all

Sample points, specified as a vector. x and v must be the same size. The sample points in x must be unique.

Data Types: single | double

Sample values, specified as a vector, matrix, or multidimensional array. The elements of v are the values that correspond to the sample points in x.

  • To interpolate using a single set of values, x and v must be vectors of the same length.

  • To interpolate using multiple sets of values, v can be an array with extra dimensions compared to x. The size of the first dimension of v must match the number of sample points in x, and each column in v defines a separate set of 1-D values. For example, if x is a column vector with 10 elements, you can specify v as a 10-by-4 matrix to interpolate using four different sets of values.

Data Types: single | double

Sample points in full grid form, specified as separate n-dimensional arrays. The sample points must be unique and sorted. You can create the arrays X1,X2,...,Xn using the ndgrid function. These arrays are all the same size, and each one is the same size as V.

Data Types: single | double

Sample points in grid vector form, specified as a cell array of grid vectors {xg1,xg2,...,xgn}. The sample points must be unique and sorted. The vectors must specify a grid that is the same size as V. In other words, size(V) = [length(xg1) length(xg2),...,length(xgn)]. Use this form as an alternative to the full grid to save memory when your grid is very large.

Data Types: single | double

Sample values, specified as an array. The elements of V are the values that correspond to the sample points. The first N dimensions of V must have the same sizes as the corresponding dimensions in the full grid of sample points, where N is the number of dimensions of the grid.

  • To interpolate using a single set of values, specify V as an array with the same size as the full grid of sample points. For example, if the sample points form a grid with size 100-by-100, you can specify the values with a matrix of the same size.

  • To interpolate using multiple sets of values, specify V as an array with extra dimensions compared to the grid of sample points. The extra dimensions define multiple values at each sample point. For example, if the sample points form a grid with size 100-by-100, you can specify the values as an array with size 100-by-100-by-4 to interpolate using four different sets of 100-by-100 values.

Data Types: single | double

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

MethodDescriptionContinuityComments
'linear' (default)Linear interpolation. The interpolated value at a query point is based on linear interpolation of the values at neighboring grid points in each respective dimension. C0
  • Requires at least 2 grid points in each dimension

  • Requires more memory than 'nearest'

'nearest'Nearest neighbor interpolation. The interpolated value at a query point is the value at the nearest sample grid point. Discontinuous
  • Requires 2 grid points in each dimension

  • Fastest computation with modest memory requirements

'next'Next neighbor interpolation (for 1-D only). The interpolated value at a query point is the value at the next sample grid point.Discontinuous
  • Requires at least 2 points

  • Same memory requirements and computation time as 'nearest'

'previous'Previous neighbor interpolation (for 1-D only). The interpolated value at a query point is the value at the previous sample grid point.Discontinuous
  • Requires at least 2 points

  • Same memory requirements and computation time as 'nearest'

'pchip'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
  • Requires at least 4 points

  • Requires more memory and computation time than 'linear'

'cubic'Cubic interpolation. 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
  • Grid must have uniform spacing, although the spacing in each dimension does not have to be the same

  • Requires at least 4 points in each dimension

  • Requires more memory and computation time than 'linear'

'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.C1
  • Requires at least 2 points in each dimension

  • Produces fewer undulations than 'spline', but does not flatten as aggressively as 'pchip'

  • Computation is more expensive than 'pchip', but typically less than 'spline'

  • Memory requirements are similar to those of 'spline'

'spline'Cubic spline interpolation. 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
  • Requires 4 points in each dimension

  • Requires more memory and computation time than 'cubic'

Extrapolation method, specified as 'linear', 'nearest', 'next', 'previous', 'pchip', 'cubic', 'spline', or 'makima'. In addition, you can specify 'none' if you want queries outside the domain of your grid to return NaN values.

If you omit ExtrapolationMethod, the default is the value you specify for Method. If you omit both the Method and ExtrapolationMethod arguments, both values default to 'linear'.

Properties

expand all

Grid vectors, specified as a cell array {xg1,xg2,...,xgn}. These vectors specify the grid points (locations) for the values in Values. The grid points must be unique.

Data Types: cell

Function values at sample points, specified as an array of values associated with the grid points in GridVectors.

Data Types: single | double

Interpolation method, specified as a character vector. Method can be: 'linear', 'nearest', 'next', 'previous', 'pchip', 'cubic', 'spline', or 'makima'. See Method for descriptions of these methods.

Data Types: char

Extrapolation method, specified as a character vector. ExtrapolationMethod can be: 'linear', 'nearest', 'next', 'previous', 'pchip', 'cubic', 'spline', 'makima', or 'none'. A value of 'none' indicates that extrapolation is disabled. The default value is the value of Method.

Data Types: char

Usage

Syntax

Vq = F(Xq)

Vq = F(xq1,xq2,...,xqn)

Vq = F(Xq1,Xq2,...,Xqn)

Vq = F({xgq1,xgq2,...,xgqn})

Description

Use griddedInterpolant to create the interpolant, F. Then you can evaluate F at specific query points using any of the following syntaxes:

Vq = F(Xq) specifies the query points in the matrix Xq. Each row of Xq contains the coordinates of a query point.

Vq = F(xq1,xq2,...,xqn) specifies the query points xq1,xq2,...,xqn as column vectors of length m, representing m points scattered in n-dimensional space.

Vq = F(Xq1,Xq2,...,Xqn) specifies the query points using the n-dimensional arrays Xq1,Xq2,...,Xqn, which define a full grid of points.

Vq = F({xgq1,xgq2,...,xgqn}) specifies the query points as grid vectors. Use this syntax to conserve memory when you want to query a large grid of points.

Examples

collapse all

1-D Interpolation

Open Live Script

Use griddedInterpolant to interpolate a 1-D data set.

Create a vector of scattered sample points v. The points are sampled at random 1-D locations between 0 and 20.

x = sort(20*rand(100,1));v = besselj(0,x);

Create a gridded interpolant object for the data. By default, griddedInterpolant uses the 'linear' interpolation method.

F = griddedInterpolant(x,v)
F = griddedInterpolant with properties: GridVectors: {[100x1 double]} Values: [100x1 double] Method: 'linear' ExtrapolationMethod: 'linear'

Query the interpolant F at 500 uniformly spaced points between 0 and 20. Plot the interpolated results (xq,vq) on top of the original data (x,v).

xq = linspace(0,20,500);vq = F(xq);plot(x,v,'ro')hold onplot(xq,vq,'.')legend('Sample Points','Interpolated Values')

Gridded data interpolation - MATLAB (1)

3-D Interpolation Using Full Grid vs. Grid Vectors

Open Live Script

Interpolate 3-D data using two methods to specify the query points.

Create and plot a 3-D data set representing the function z(x,y)=sin(x2+y2)x2+y2 evaluated at a set of gridded sample points in the range [-5,5].

[x,y] = ndgrid(-5:0.8:5);z = sin(x.^2 + y.^2) ./ (x.^2 + y.^2);surf(x,y,z)

Gridded data interpolation - MATLAB (2)

Create a gridded interpolant object for the data.

Use a finer mesh to query the interpolant and improve the resolution.

[xq,yq] = ndgrid(-5:0.1:5);vq = F(xq,yq);surf(xq,yq,vq)

Gridded data interpolation - MATLAB (3)

In cases where there are a lot of sample points or query points, and where memory usage becomes a concern, you can use grid vectors to improve memory usage.

  • When you specify grid vectors instead of using ndgrid to create the full grid, griddedInterpolant avoids forming the full query grid to carry out the calculations.

  • When you pass grid vectors, they are normally grouped together as cells in a cell array, {xg1, xg2, ..., xgn}. The grid vectors are a compact way to represent the points of the full grid.

Gridded data interpolation - MATLAB (4)

Alternatively, execute the previous commands using grid vectors.

x = -5:0.8:5;y = x';z = sin(x.^2 + y.^2) ./ (x.^2 + y.^2);F = griddedInterpolant({x,y},z);xq = -5:0.1:5;yq = xq';vq = F({xq,yq});surf(xq,yq,vq)

Gridded data interpolation - MATLAB (5)

Interpolation with Default Grid

Open Live Script

Use the default grid to perform a quick interpolation on a set of sample points. The default grid uses unit-spaced points, so this interpolation is useful when the exact xy spacing between the sample points is not important.

Create a matrix of sample function values and plot them against the default grid.

x = (1:0.3:5)';y = x';V = cos(x) .* sin(y);n = length(x);surf(1:n,1:n,V)

Gridded data interpolation - MATLAB (6)

Interpolate the data using the default grid.

F = griddedInterpolant(V)
F = griddedInterpolant with properties: GridVectors: {[1 2 3 4 5 6 7 8 9 10 11 12 13 14] [1 2 3 4 5 6 7 8 9 10 11 12 13 14]} Values: [14x14 double] Method: 'linear' ExtrapolationMethod: 'linear'

Query the interpolant and plot the results.

[xq,yq] = ndgrid(1:0.2:n);Vq = F(xq,yq);surf(xq',yq',Vq)

Gridded data interpolation - MATLAB (7)

2-D Interpolation over Finer Grid

Open Live Script

Interpolate coarsely sampled data using a full grid with spacing of 0.5.

Define the sample points as a full grid with range [1, 10] in both dimensions.

[X,Y] = ndgrid(1:10,1:10);

Sample f(x,y)=x2+y2 at the grid points.

V = X.^2 + Y.^2;

Create the interpolant, specifying cubic interpolation.

F = griddedInterpolant(X,Y,V,'cubic');

Define a full grid of query points with 0.5 spacing and evaluate the interpolant at those points. Then plot the result.

[Xq,Yq] = ndgrid(1:0.5:10,1:0.5:10);Vq = F(Xq,Yq);mesh(Xq,Yq,Vq);

Gridded data interpolation - MATLAB (8)

1-D Extrapolation

Open Live Script

Compare results of querying the interpolant outside the domain of F using the 'pchip' and 'nearest' extrapolation methods.

Create the interpolant, specifying 'pchip' as the interpolation method and 'nearest' as the extrapolation method.

x = [1 2 3 4 5];v = [12 16 31 10 6];F = griddedInterpolant(x,v,'pchip','nearest')
F = griddedInterpolant with properties: GridVectors: {[1 2 3 4 5]} Values: [12 16 31 10 6] Method: 'pchip' ExtrapolationMethod: 'nearest'

Query the interpolant, and include points outside the domain of F.

xq = 0:0.1:6;vq = F(xq);figureplot(x,v,'o',xq,vq,'-b');legend ('v','vq')

Gridded data interpolation - MATLAB (9)

Query the interpolant at the same points again, this time using the 'pchip' extrapolation method.

F.ExtrapolationMethod = 'pchip';figurevq = F(xq);plot(x,v,'o',xq,vq,'-b');legend ('v','vq')

Gridded data interpolation - MATLAB (10)

Interpolate Multiple Sets of Values on Same Grid

Open Live Script

Use griddedInterpolant to interpolate three different sets of values at the same query points.

Create a grid of sample points with -5X5 and -3Y3.

gx = -5:5;gy = -3:3;[X,Y] = ndgrid(gx,gy);

Evaluate three different functions at the query points, and then concatenate the values into a 3-D array. The size of V is the same as the X and Y grids in the first two dimensions, but the size of the extra dimension reflects the number of values associated with each sample point (in this case, three).

f1 = X.^2 + Y.^2;f2 = X.^3 + Y.^3;f3 = X.^4 + Y.^4;V = cat(3,f1,f2,f3);

Create an interpolant using the sample points and associated values.

F = griddedInterpolant(X,Y,V);

Create a grid of query points with a finer mesh size compared to the sample points.

qx = -5:0.4:5;qy = -3:0.4:3;[XQ,YQ] = ndgrid(qx,qy);

Interpolate all three sets of values at the query points.

VQ = F(XQ,YQ);

Compare the original data with the interpolated results.

tiledlayout(3,2)nexttilesurf(X,Y,f1)title('f1')nexttilesurf(XQ,YQ,VQ(:,:,1))title('Interpolated f1')nexttilesurf(X,Y,f2)title('f2')nexttilesurf(XQ,YQ,VQ(:,:,2))title('Interpolated f2')nexttilesurf(X,Y,f3)title('f3')nexttilesurf(XQ,YQ,VQ(:,:,3))title('Interpolated f3')

Gridded data interpolation - MATLAB (11)

More About

expand all

Interpolating function that you can evaluate at query points.

A set of points that are axis-aligned and ordered.

A set of points that have no structure among their relative locations.

A grid represented as a set of arrays. For example, you can create a full grid using ndgrid.

Tips

  • It is quicker to evaluate a griddedInterpolant object F at many different sets of query points than it is to compute the interpolations separately using interp1, interp2, interp3, or interpn. For example:

    % Fast to create interpolant F and evaluate multiple timesF = griddedInterpolant(X1,X2,V)v1 = F(Xq1)v2 = F(Xq2)% Slower to compute interpolations separately using interp2v1 = interp2(X1,X2,V,Xq1)v2 = interp2(X1,X2,V,Xq2)

Extended Capabilities

Version History

Introduced in R2011b

expand all

See Also

scatteredInterpolant | interp1 | interp2 | interp3 | interpn | ndgrid | meshgrid | fillmissing | filloutliers

Topics

  • Resample Image with Gridded Interpolation
  • Interpolating Gridded Data

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.

Gridded data interpolation - MATLAB (12)

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)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本 (日本語)
  • 한국 (한국어)

Contact your local office

Gridded data interpolation - MATLAB (2024)
Top Articles
Latest Posts
Article information

Author: Ray Christiansen

Last Updated:

Views: 5523

Rating: 4.9 / 5 (69 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Ray Christiansen

Birthday: 1998-05-04

Address: Apt. 814 34339 Sauer Islands, Hirtheville, GA 02446-8771

Phone: +337636892828

Job: Lead Hospitality Designer

Hobby: Urban exploration, Tai chi, Lockpicking, Fashion, Gunsmithing, Pottery, Geocaching

Introduction: My name is Ray Christiansen, I am a fair, good, cute, gentle, vast, glamorous, excited person who loves writing and wants to share my knowledge and understanding with you.