Interpolation for 3-D gridded data in meshgrid format (2024)

Interpolation for 3-D gridded data in meshgrid format

collapse all in page

Syntax

Vq = interp3(X,Y,Z,V,Xq,Yq,Zq)

Vq = interp3(V,Xq,Yq,Zq)

Vq = interp3(V)

Vq = interp3(V,k)

Vq = interp3(___,method)

Vq = interp3(___,method,extrapval)

Description

example

Vq = interp3(X,Y,Z,V,Xq,Yq,Zq) returnsinterpolated values of a function of three variables at specific querypoints using linear interpolation. The results always pass throughthe original sampling of the function. X, Y,and Z contain the coordinates of the sample points. V containsthe corresponding function values at each sample point. Xq, Yq,and Zq contain the coordinates of the query points.

Vq = interp3(V,Xq,Yq,Zq) assumesa default grid of sample points. The default grid points cover theregion, X=1:n, Y=1:m, Z=1:p,where [m,n,p] = size(V). Use this syntax when youwant to conserve memory and are not concerned about the absolutedistances between points.

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

Vq = interp3(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 = interp3(___,method) specifies an alternative interpolation method: 'linear', 'nearest', 'cubic', 'makima', or 'spline'. The default method is 'linear'.

example

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

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

  • NaN values for other interpolation methods

Examples

collapse all

Interpolate Using Default Method

Open Live Script

Load the points and values of the flow function, sampled at 10 points in each dimension.

[X,Y,Z,V] = flow(10);

The flow function returns the grid in the arrays, X, Y, Z. The grid covers the region, 0.1X10, -3Y3, -3Z3, and the spacing is ΔX=0.5, ΔY=0.7, and ΔZ=0.7.

Now, plot slices through the volume of the sample at: X=6, X=9, Y=2, and Z=0.

figureslice(X,Y,Z,V,[6 9],2,0);shading flat

Interpolation for 3-D gridded data in meshgrid format (1)

Create a query grid with spacing of 0.25.

[Xq,Yq,Zq] = meshgrid(.1:.25:10,-3:.25:3,-3:.25:3);

Interpolate at the points in the query grid and plot the results using the same slice planes.

Interpolation for 3-D gridded data in meshgrid format (2)

Interpolate Using Cubic Method

Open Live Script

Load the points and values of the flow function, sampled at 10 points in each dimension.

[X,Y,Z,V] = flow(10);

The flow function returns the grid in the arrays, X, Y, Z. The grid covers the region, 0.1X10, -3Y3, -3Z3, and the spacing is ΔX=0.5, ΔY=0.7, and ΔZ=0.7.

Plot slices through the volume of the sample at: X=6, X=9, Y=2, and Z =0.

figureslice(X,Y,Z,V,[6 9],2,0);shading flat

Interpolation for 3-D gridded data in meshgrid format (3)

Create a query grid with spacing of 0.25.

[Xq,Yq,Zq] = meshgrid(.1:.25:10,-3:.25:3,-3:.25:3);

Interpolate at the points in the query grid using the 'cubic' interpolation method. Then plot the results.

Vq = interp3(X,Y,Z,V,Xq,Yq,Zq,'cubic');figureslice(Xq,Yq,Zq,Vq,[6 9],2,0);shading flat

Interpolation for 3-D gridded data in meshgrid format (4)

Evaluate Outside the Domain of X, Y, and Z

Open Live Script

Create the grid vectors, x, y, and z. These vectors define the points associated with values in V.

x = 1:100;y = (1:50)';z = 1:30;

Define the sample values to be a 50-by-100-by-30 random number array, V. Use the rand function to create the array.

rng('default')V = rand(50,100,30);

Evaluate V at three points outside the domain of x, y, and z. Specify extrapval = -1.

xq = [0 0 0];yq = [0 0 51];zq = [0 101 102];vq = interp3(x,y,z,V,xq,yq,zq,'linear',-1)
vq = 1×3 -1 -1 -1

All three points evaluate to -1 because they are outside the domain of x, y, and z.

Input Arguments

collapse all

X,Y,ZSample grid points
arrays | vectors

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

  • If X, Y, and Z arearrays, then they contain the coordinates of a full grid (in meshgrid format).Use the meshgrid function tocreate the X, Y, and Z arraystogether. These arrays must be the same size.

  • If X, Y, and Z are vectors, then they are treated as a grid vectors. The values in these vectors must be strictly monotonic, either increasing or decreasing.

Example: [X,Y,Z] = meshgrid(1:30,-10:10,1:5)

Data Types: single | double

VSample values
array

Sample values, specified as a real or complex array. The sizerequirements for V depend on the size of X, Y,and Z:

  • If X, Y, and Z are arrays representing a full grid (in meshgrid format), then the size of V matches the size of X, Y, or Z.

  • If X, Y, and Z aregrid vectors, then size(V) = [length(Y) length(X) length(Z)].

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

Example: rand(10,10,10)

Data Types: single | double
Complex Number Support: Yes

Xq,Yq,ZqQuery points
scalars | vectors | arrays

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

  • If Xq, Yq, and Zq arescalars, then they are the coordinates of a single query point in R3.

  • If Xq, Yq, and Zq arevectors of different orientations, then Xq, Yq,and Zq are treated as grid vectors in R3.

  • If Xq, Yq, and Zq arevectors of the same size and orientation, then Xq, Yq,and Zq are treated as scattered points in R3.

  • If Xq, Yq, and Zq arearrays of the same size, then they represent either a full grid ofquery points (in meshgrid format) or scatteredpoints in R3.

Example: [Xq,Yq,Zq] = meshgrid((1:0.1:10),(-5:0.1:0),3:5)

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.

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

The following illustration depicts k=2 inone plane of R3.There are 72 interpolated values in red and 9 sample values in black.

Interpolation for 3-D gridded data in meshgrid format (5)

Example: interp3(V,2)

Data Types: single | double

methodInterpolation method
'linear' (default) | 'nearest' | '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

'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 X, Y, and Z
scalar

Function value outside domain of X, Y,and Z, specified as a real or complex scalar. interp3 returnsthis constant value for all points outside the domain of X, Y,and Z.

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 thesyntax you use and, in some cases, the size and value of the inputarguments.

SyntaxesSpecialConditionsSize of VqExample
interp3(X,Y,Z,V,Xq,Yq,Zq)
interp3(V,Xq,Yq,Zq)
and variations of these syntaxes that include method or extrapval
Xq, Yq, and Zq arescalars.Scalarsize(Vq) = [1 1] when you pass Xq, Yq,and Zq as scalars.
Same as aboveXq, Yq, and Zq arevectors of the same size and orientation.Vector of same size and orientation as Xq, Yq,and ZqIf size(Xq) = [100 1],
and size(Yq)= [100 1],
and size(Zq) = [1001],
then size(Vq) = [100 1].
Same as aboveXq, Yq, and Zq arevectors of mixed orientation.size(Vq) = [length(Y) length(X) length(Z)]If size(Xq) = [1 100],
and size(Yq)= [50 1],
and size(Zq) = [1 5],
then size(Vq) = [50 100 5].
Same as aboveXq, Yq, and Zq arearrays of the same size.Array of the same size as Xq, Yq,and ZqIf size(Xq) = [50 25],
and size(Yq)= [50 25],
and size(Zq) = [50 25],
then size(Vq) = [50 25].
interp3(V,k)
and variationsof this syntax that include method or extrapval
None

Array in which the length of the ithdimension is
2^k * (size(V,i)-1)+1

If size(V) = [10 12 5],
and k= 3,
then size(Vq) = [73 89 33].

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

For interp3, a full gridconsists of three arrays whose elements represent a grid of pointsthat define a region in R3.The first array contains the x-coordinates, thesecond array contains the y-coordinates, and thethird array contains the z-coordinates. The valuesin each array vary along a single dimension and are constant alongthe other dimensions.

The values in the x-array are strictly monotonic,increasing, and vary along the second dimension. The values in the y-arrayare strictly monotonic, increasing, and vary along the first dimension.The values in the z-array are strictly monotonic,increasing, and vary along the third dimension. Use the meshgrid function to create a full gridthat you can pass to interp3.

Grid Vectors

For interp3, grid vectorsconsist of three vectors of mixed-orientation that define the pointson a grid in R3.

For example, the following code creates the grid vectors forthe region, 1 ≤ x ≤ 3, 4 ≤ y ≤5, and 6 ≤ z ≤ 8:

x = 1:3;y = (4:5)';z = 6:8;

Scattered Points

For interp3, scatteredpoints consist of three arrays or vectors, Xq, Yq,and Zq, that define a collection of points scatteredin R3.The ith array contains the 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).

Xq = [1 6; 15 0];Yq = [19 40; 33 61];Zq = [10 1; 22 13];

Extended Capabilities

Version History

Introduced before R2006a

See Also

interp1 | interp2 | interpn | meshgrid

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 3-D gridded data in meshgrid format (6)

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

Contact your local office

Interpolation for 3-D gridded data in meshgrid format (2024)
Top Articles
Latest Posts
Article information

Author: Domingo Moore

Last Updated:

Views: 5959

Rating: 4.2 / 5 (73 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Domingo Moore

Birthday: 1997-05-20

Address: 6485 Kohler Route, Antonioton, VT 77375-0299

Phone: +3213869077934

Job: Sales Analyst

Hobby: Kayaking, Roller skating, Cabaret, Rugby, Homebrewing, Creative writing, amateur radio

Introduction: My name is Domingo Moore, I am a attractive, gorgeous, funny, jolly, spotless, nice, fantastic person who loves writing and wants to share my knowledge and understanding with you.