Interpolation for 1-D, 2-D, 3-D, and N-D gridded data in ndgrid format (2024)

Interpolation for 1-D, 2-D, 3-D, and N-D gridded datain ndgrid format

collapse all in page

Syntax

Vq = interpn(X1,X2,...,Xn,V,Xq1,Xq2,...,Xqn)

Vq = interpn(V,Xq1,Xq2,...,Xqn)

Vq = interpn(V)

Vq = interpn(V,k)

Vq = interpn(___,method)

Vq = interpn(___,method,extrapval)

Description

example

Vq = interpn(X1,X2,...,Xn,V,Xq1,Xq2,...,Xqn) returnsinterpolated values of a function of n variablesat specific query points using linear interpolation. The results alwayspass through the original sampling of the function. X1,X2,...,Xn containthe coordinates of the sample points. V containsthe corresponding function values at each sample point. Xq1,Xq2,...,Xqn containthe coordinates of the query points.

Vq = interpn(V,Xq1,Xq2,...,Xqn) assumes a default grid of sample points. The default grid consists of the points, 1,2,3,...ni in each dimension. The value of ni is the length of the ith dimension in V. Use this syntax when you want to conserve memory and are not concerned about the absolute distances between points.

Vq = interpn(V) returnsthe interpolated values on a refined grid formed by dividing the intervalbetween sample values once in each dimension.

example

Vq = interpn(V,k) returnsthe interpolated values on a refined grid formed by repeatedly halvingthe intervals k times in each dimension. This resultsin 2^k-1 interpolated points between sample values.

example

Vq = interpn(___,method) specifies an alternative interpolation method: 'linear', 'nearest', 'pchip','cubic', 'makima', or 'spline'. The default method is 'linear'.

example

Vq = interpn(___,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 interpn returnsone of the following:

  • The extrapolated values for the 'spline' and 'makima' methods

  • NaN values for other interpolation methods

Examples

collapse all

1-D Interpolation

Open Live Script

Define the sample points and values.

x = [1 2 3 4 5];v = [12 16 31 10 6];

Define the query points, xq, and interpolate.

xq = (1:0.1:5);vq = interpn(x,v,xq,'cubic');

Plot the result.

figureplot(x,v,'o',xq,vq,'-');legend('Samples','Cubic Interpolation');

Interpolation for 1-D, 2-D, 3-D, and N-D gridded datain ndgrid format (1)

2-D Interpolation

Open Live Script

Create a set of grid points and corresponding sample values.

[X1,X2] = ndgrid((-5:1:5));R = sqrt(X1.^2 + X2.^2)+ eps;V = sin(R)./(R);

Interpolate over a finer grid using ntimes=1.

Vq = interpn(V,'cubic');mesh(Vq);

Interpolation for 1-D, 2-D, 3-D, and N-D gridded datain ndgrid format (2)

Interpolate Two Sets of 2-D Sample Values

Open Live Script

Create a grid of 2-D sample points using ndgrid.

[x,y] = ndgrid(0:10,0:5);

Create two different sets of sample values at the sample points and concatenate them as pages in a 3-D array. Plot the two sets of sample values against the sample points. Because surf uses meshgrid format for grids, transpose the inputs for plotting.

v1 = sin(x.*y)./(x+1);v2 = x.*erf(y);V = cat(3,v1,v2);tiledlayout(1,2)nexttilesurf(x',y',V(:,:,1)')view(2)nexttilesurf(x',y',V(:,:,2)')view(2)

Interpolation for 1-D, 2-D, 3-D, and N-D gridded datain ndgrid format (3)

Create a set of query points for interpolation using ndgrid and then use interpn to find the values of each function at the query points. Plot the interpolated values against the query points.

[xq,yq] = ndgrid(0:0.2:10);Vq = interpn(x,y,V,xq,yq);tiledlayout(1,2)nexttilesurf(xq',yq',Vq(:,:,1)')view(2)nexttilesurf(xq',yq',Vq(:,:,2)')view(2)

Interpolation for 1-D, 2-D, 3-D, and N-D gridded datain ndgrid format (4)

Evaluate Outside Domain of 3-D Function

Open Live Script

Create the grid vectors, x1, x2, and x3. These vectors define the points associated with the values in V.

x1 = 1:100;x2 = 1:50;x3 = 1:30;

Define the sample values to be a 100-by-50-by-30 array of random numbers, V.

rng defaultV = rand(100,50,30);

Evaluate V at three points outside the domain of x1, x2, and x3. Specify extrapval = -1.

xq1 = [0 0 0];xq2 = [0 0 51];xq3 = [0 101 102];vq = interpn(x1,x2,x3,V,xq1,xq2,xq3,'linear',-1)
vq = 1×3 -1 -1 -1

All three points evaluate to -1 because they are outside the domain of x1, x2, and x3.

4-D Interpolation

Open Script

Define an anonymous function that represents Interpolation for 1-D, 2-D, 3-D, and N-D gridded datain ndgrid format (5).

f = @(x,y,z,t) t.*exp(-x.^2 - y.^2 - z.^2);

Create a grid of points in Interpolation for 1-D, 2-D, 3-D, and N-D gridded datain ndgrid format (6). Then, pass the points through the function to create the sample values, V.

[x,y,z,t] = ndgrid(-1:0.2:1,-1:0.2:1,-1:0.2:1,0:2:10);V = f(x,y,z,t);

Now, create the query grid.

[xq,yq,zq,tq] = ...ndgrid(-1:0.05:1,-1:0.08:1,-1:0.05:1,0:0.5:10);

Interpolate V at the query points.

Vq = interpn(x,y,z,t,V,xq,yq,zq,tq);

Create a movie to show the results.

figure;nframes = size(tq, 4);for j = 1:nframes slice(yq(:,:,:,j),xq(:,:,:,j),zq(:,:,:,j),... Vq(:,:,:,j),0,0,0); clim([0 10]); M(j) = getframe;endmovie(M);

Interpolation for 1-D, 2-D, 3-D, and N-D gridded datain ndgrid format (7)

Input Arguments

collapse all

X1,X2,...,XnSample grid points
arrays | vectors

Sample grid points, specified as real arrays or vectors. Thesample grid points must be unique.

  • If X1,X2,...,Xn are arrays, thenthey contain the coordinates of a full grid (in ndgrid format).Use the ndgrid function tocreate the X1,X2,...,Xn arrays together. Thesearrays must be the same size.

  • If X1,X2,...,Xn are vectors, then they are treated as grid vectors. The values in these vectors must be strictly monotonic, either increasing or decreasing.

Example: [X1,X2,X3,X4] = ndgrid(1:30,-10:10,1:5,10:13)

Data Types: single | double

VSample values
array

Sample values, specified as a real or complex array. The size requirements for V depend on the size of the grid of sample points defined by X1,X2,...,Xn. The sample points X1,X2,...,Xn can be arrays or grid vectors, but in both cases they define an n-dimensional grid. V must be an array that at least has the same n dimension sizes, but it also can have extra dimensions beyond n:

  • If V also has n dimensions, then the size of V must match the size of the n-dimensional grid defined by X1,X2,...,Xn. In this case, V contains one set of sample values at the sample points. For example, if X1,X2,X3 are 3-by-3-by-3 arrays, then V can also be a 3-by-3-by-3 array.

  • If V has more than n dimensions, then the first n dimensions of V must match the size of the n-dimensional grid defined by X1,X2,...,Xn. The extra dimensions in V define extra sets of sample values at the sample points. For example, if X1,X2,X3 are 3-by-3-by-3 arrays, then V can be a 3-by-3-by-3-by-2 array to define two sets of sample values at the sample points.

If V contains complex numbers, then interpn interpolatesthe real and imaginary parts separately.

Example: rand(10,5,3,2)

Data Types: single | double
Complex Number Support: Yes

Xq1,Xq2,...,XqnQuery points
scalars | vectors | arrays

Query points, specified as real scalars, vectors, or arrays.

  • If Xq1,Xq2,...,Xqn are scalars,then they are the coordinates of a single query point in Rn.

  • If Xq1,Xq2,...,Xqn are vectorsof different orientations, then Xq1,Xq2,...,Xqn aretreated as grid vectors in Rn.

  • If Xq1,Xq2,...,Xqn are vectorsof the same size and orientation, then Xq1,Xq2,...,Xqn aretreated as scattered points in Rn.

  • If Xq1,Xq2,...,Xqn are arrays ofthe same size, then they represent either a full grid of query points(in ndgrid format) or scattered points in Rn.

Example: [X1,X2,X3,X4] = ndgrid(1:10,1:5,7:9,10:11)

Data Types: single | double

kRefinement 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^k-1 interpolated points between sample values.

If k is 0, then Vq isthe same as V.

interpn(V,1) is the same as interpn(V).

The following illustration depicts k=2 in R2.There are 72 interpolated values in red and 9 sample values in black.

Interpolation for 1-D, 2-D, 3-D, and N-D gridded datain ndgrid format (8)

Example: interpn(V,2)

Data Types: single | double

methodInterpolation method
'linear' (default) | 'nearest' | 'pchip' | 'cubic' | 'spline' | 'makima'

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

MethodDescriptionContinuityComments
'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.C0
  • Requires at least two grid points in each dimension

  • Requires more memory than 'nearest'

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

  • Fastest computation with modest memory requirements

'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 four points

  • Requires more memory and computation time than 'linear'

'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.C1
  • Grid must have uniform spacing in each dimension, but the spacing does not have to be the same for all dimensions

  • Requires at least four 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'

  • Computation time is typically less than 'spline', but the memory requirements are similar

'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 not-a-knot end conditions.C2
  • Requires four points in each dimension

  • Requires more memory and computation time than 'cubic'

extrapvalFunction value outside domain of X1,X2,...,Xn
scalar

Function value outside domain of X1,X2,...,Xn,specified as a real or complex scalar. interpn returnsthis constant value for all points outside the domain of X1,X2,...,Xn.

Example: 5

Example: 5+1i

Data Types: single | double
Complex Number Support: Yes

Output Arguments

collapse all

Vq — Interpolated values
scalar | vector | array

Interpolated values, returned as a real or complex scalar, vector, or array. The size and shape of Vq depends on the syntax you use and, in some cases, the size and value of the input arguments.

  • If you specify sample points with X1,X2,...,Xn, or use the default grid, and V has the same number of dimensions as the n-dimensional grid of sample points, then Vq contains a single set of interpolated values at the query points defined by Xq1,Xq2,...,Xqn.

    • If Xq1,Xq2,...,Xqn are scalars, then Vq is a scalar.

    • If Xq1,Xq2,...,Xqn are vectors of the same size and orientation, then Vq is a vector with the same size and orientation.

    • If Xq1,Xq2,...,Xqn are grid vectors of mixed orientation, then Vq is an array with the same size as the grid implicitly defined by the grid vectors.

    • If Xq1,Xq2,...,Xqn are arrays of the same size, then Vq is an array with the same size.

  • If you specify sample points with X1,X2,...,Xn, or use the default grid, and V has more dimensions than the n-dimensional grid of sample points, then Vq contains multiple sets of interpolated values at the query points defined by Xq1,Xq2,...,Xqn. In this case, the first n dimensions of Vq follow the size rules for a single set of interpolated values above, but Vq also has the same extra dimensions as V with the same sizes.

  • With the syntaxes interpn(V) and interpn(V,k), the interpolation is performed by subdividing the default grid k times (where k=1 for interpn(V)). In this case, Vq is an array with the same number of dimensions as V where the size of the ith dimension is 2^k * (size(V,i)-1)+1.

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 ndgrid Format)

For interpn, the fullgrid consists of n arrays, X1,X2,...,Xn,whose elements represent a grid of points in Rn.The ith array, Xi, contains strictly monotonic,increasing values that vary most rapidly along the ith dimension.

Use the ndgrid functionto create a full grid that you can pass to interpn.For example, the following code creates a full grid in R2 forthe region, 1 ≤ X1 ≤ 3, 1≤ X2 ≤4.

[X1,X2] = ndgrid(-1:3,(1:4))
X1 = -1 -1 -1 -1 0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3X2 = 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4

Grid Vectors

For interpn, grid vectorsconsist of n vectors of mixed-orientation thatdefine the points of a grid in Rn.

For example, the following code creates the grid vectors in R3 forthe region, 1 ≤ x1 ≤ 3, 4 ≤ x2 ≤5, and 6 ≤x3≤ 8:

x1 = 1:3;x2 = (4:5)';x3 = 6:8;

Scattered Points

For interpn, scatteredpoints consist of n arrays or vectors, Xq1,Xq2,...,Xqn,that define a collection of points scattered in Rn.The ith array, Xi, containsthe coordinates in the ith dimension.

For example, the following code specifies the points, (1, 19,10), (6, 40, 1), (15, 33, 22), and (0, 61, 13) in R3.

Xq1 = [1 6; 15 0];Xq2 = [19 40; 33 61];Xq3 = [10 1; 22 13];

Extended Capabilities

Version History

Introduced before R2006a

expand all

Support added to interpolate multiple data sets on the same grid at the same query points. For example, if you specify a 2-D grid, a 3-D array of values at the grid points, and a 2-D collection of query points, then interpn returns the interpolated values at the query points for each 2-D page in the 3-D array of values.

See Also

interp1 | interp2 | interp3 | 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.

Interpolation for 1-D, 2-D, 3-D, and N-D gridded datain ndgrid format (9)

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

Interpolation for 1-D, 2-D, 3-D, and N-D gridded data
in ndgrid format (2024)

FAQs

How to interpolate gridded data in Matlab? ›

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) .

What is the best way to interpolate data? ›

Linear interpolation is the most straightforward and commonly used interpolation method. It comes naturally when we have two points, we connect them with a straight line to fill out the missing information in between. By doing so, we made our assumption that the points on the line represent the unobserved values.

What is the best data interpolation method? ›

Radial Basis Function interpolation is a diverse group of data interpolation methods. In terms of the ability to fit your data and produce a smooth surface, the Multiquadric method is considered by many to be the best.

How to interpolate 1d data in MATLAB? ›

vq = interp1( x , v , xq ) returns interpolated values of a 1-D function at specific query points using linear interpolation. Vector x contains the sample points, and v contains the corresponding values, v(x). Vector xq contains the coordinates of the query points.

What is the difference between griddedInterpolant and interp2? ›

griddedInterpolant offers substantial performance improvements for repeated queries of the interpolant object, whereas the interp functions perform a new calculation each time they are called.

What is the simplest interpolation method? ›

One of the simplest methods, linear interpolation, requires knowledge of two points and the constant rate of change between them. With this information, you may interpolate values anywhere between those two points.

Is it better to interpolate or extrapolate? ›

Interpolation often provides a valid estimate of an unknown value, which is why it's considered a more reliable estimation method than extrapolation.

What is the fastest interpolation method? ›

The Nearest Point interpolation method is the fastest of all the interpolation methods when used with point data (fig. 19). If used with line or polygon data it can be slower than the Nearest interpolation especially if many of the object vertices lie outside the grid.

How do you do interpolation formula? ›

The linear interpolation formula, or interpolation equation, appears as follows: y − y 1 = y 2 − y 1 x 2 − x 1 ( x − x 1 ) , where ( x 1 , y 1 ) and ( x 2 , y 2 ) are two known data points and ("x," "y") represents the data point to be estimated. The image below illustrates linear interpolation.

Which interpolation formula is better? ›

If linear interpolation formula is concerned then it can be used to find the new value from the two given points. If we compare it to Lagrange's interpolation formula, the “n” set of numbers is needed. Thereafter Lagrange's method is to be used to find the new value.

What are the two main types of interpolation approach? ›

There are several formal kinds of interpolation, including linear interpolation, polynomial interpolation, and piecewise constant interpolation.

What is optimal interpolation technique? ›

Optimal interpolation is a commonly used method for data assimilation. It combines theory (background data) with observations. It can be used to merge and interpolate measurements of the same variable, such as elevation or precipitation, from different data sources.

How do you interpolate data on a graph? ›

We could use our graph to interpolate the volume for a sample with a mass of 2.5 g. This is done by drawing a vertical line from the x-axis at a value of 2.5 g until it crosses our best fit line, and then drawing a horizontal line to the y-axis. The y-value at this point, 4.9 ml, is equal to the volume of 2.5 g sample.

Which MATLAB function will be useful for interpolating a gridded data on some 3D surface? ›

Vq = interp3( X,Y,Z , V , Xq,Yq,Zq ) returns interpolated values of a function of three variables at specific query points using linear interpolation. The results always pass through the original sampling of the function. X , Y , and Z contain the coordinates of the sample points.

How do you interpolate a curve in MATLAB? ›

Selecting an Interpolant Fit

On the Curve Fitter tab, in the Fit Type section, select an Interpolant fit. The app fits an interpolating curve or surface that passes through every data point. In the Fit Options pane, you can specify the Interpolation method value.

What is kriging interpolation in MATLAB? ›

kriging uses ordinary kriging to interpolate a variable z measured at locations with the coordinates x and y at unsampled locations xi, yi. The function requires the variable vstruct that contains all necessary information on the variogram. vstruct is the forth output argument of the function variogramfit.

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Roderick King

Last Updated:

Views: 6247

Rating: 4 / 5 (71 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Roderick King

Birthday: 1997-10-09

Address: 3782 Madge Knoll, East Dudley, MA 63913

Phone: +2521695290067

Job: Customer Sales Coordinator

Hobby: Gunsmithing, Embroidery, Parkour, Kitesurfing, Rock climbing, Sand art, Beekeeping

Introduction: My name is Roderick King, I am a cute, splendid, excited, perfect, gentle, funny, vivacious person who loves writing and wants to share my knowledge and understanding with you.