Main Content

interp3

Interpolation for 3-D gridded data in meshgrid format

Description

example

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, andZcontain the coordinates of the sample points.Vcontains the corresponding function values at each sample point.Xq,Yq, andZqcontain the coordinates of the query points.

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

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

Vq= interp3(V,k)returns the interpolated values on a refined grid formed by repeatedly halving the intervalsktimes in each dimension. This results in2^k-1interpolated points between sample values.

example

Vq= interp3(___,method)指定一个替代插值方法:'linear','nearest','cubic','makima', or'spline'.The default method is'linear'

example

Vq= interp3(___,method,extrapval)also specifiesextrapval, a scalar value that is assigned to all queries that lie outside the domain of the sample points.

If you omit theextrapvalargument for queries outside the domain of the sample points, then based on themethodargumentinterp3returns one of the following:

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

  • NaNvalues for other interpolation methods

Examples

collapse all

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

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

Theflowfunction returns the grid in the arrays,X,Y,Z.The grid covers the region, 0 1 X 1 0 , - 3 Y 3 , - 3 Z 3 , 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, andZ=0

figure slice(X,Y,Z,V,[6 9],2,0); shadingflat

Figure contains an axes object. The axes object contains 4 objects of type surface.

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.

Vq = interp3(X,Y,Z,V,Xq,Yq,Zq); figure slice(Xq,Yq,Zq,Vq,[6 9],2,0); shadingflat

Figure contains an axes object. The axes object contains 4 objects of type surface.

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

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

Theflowfunction returns the grid in the arrays,X,Y,Z.The grid covers the region, 0 1 X 1 0 , - 3 Y 3 , - 3 Z 3 , 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, andZ =0

figure slice(X,Y,Z,V,[6 9],2,0); shadingflat

Figure contains an axes object. The axes object contains 4 objects of type surface.

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'); figure slice(Xq,Yq,Zq,Vq,[6 9],2,0); shadingflat

Figure contains an axes object. The axes object contains 4 objects of type surface.

Create the grid vectors,x,y, andz.These vectors define the points associated with values inV

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 therandfunction to create the array.

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

EvaluateVat three points outside the domain ofx,y, andz.Specifyextrapval = -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-1because they are outside the domain ofx,y, andz

输入参数

collapse all

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

  • IfX,Y, andZare arrays, then they contain the coordinates of afull grid (in meshgrid format).Use themeshgridfunction to create theX,Y, andZarrays together. These arrays must be the same size.

  • IfX,Y, andZare vectors, then they are treated as agrid vectors.的值in these vectors must bestrictly monotonic, either increasing or decreasing.

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

Data Types:single|double

Sample values, specified as a real or complex array. The size requirements forVdepend on the size ofX,Y, andZ:

  • IfX,Y, andZare arrays representing a full grid (inmeshgridformat), then the size ofVmatches the size ofX,Y, orZ

  • IfX,Y, andZare grid vectors, thensize(V) = [length(Y) length(X) length(Z)]

IfVcontains complex numbers, theninterp3interpolates the real and imaginary parts separately.

Example:rand(10,10,10)

Data Types:single|double
Complex Number Support:Yes

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

  • IfXq,Yq, andZqare scalars, then they are the coordinates of a single query point inR3

  • IfXq,Yq, andZqare vectors of different orientations, thenXq,Yq, andZqare treated as grid vectors inR3

  • IfXq,Yq, andZqare vectors of the same size and orientation, thenXq,Yq, andZqare treated asscattered pointsinR3

  • IfXq,Yq, andZqare arrays of the same size, then they represent either a full grid of query points (inmeshgridformat) or scattered points inR3

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

Data Types:single|double

Refinement factor, specified as a real, nonnegative, integer scalar. This value specifies the number of times to repeatedly divide the intervals of the refined grid in each dimension. This results in2^k-1interpolated points between sample values.

Ifkis0, thenVqis the same asV

interp3(V,1)is the same asinterp3(V)

The following illustration depictsk=2in one plane ofR3.There are 72 interpolated values in red and 9 sample values in black.

Example:interp3(V,2)

Data Types:single|double

插值法, specified as one of the options in this table.

Method Description Continuity Comments
'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'

Function value outside domain ofX,Y, andZ, specified as a real or complex scalar.interp3returns this constant value for all points outside the domain ofX,Y, andZ

Example:5

Example:5+1i

Data Types:single|double
Complex Number Support:Yes

Output Arguments

collapse all

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

Syntaxes Special Conditions Size of Vq Example
interp3(X,Y,Z,V,Xq,Yq,Zq)
interp3(V,Xq,Yq,Zq)
and variations of these syntaxes that includemethodorextrapval
Xq,Yq, andZqare scalars. Scalar size(Vq) = [1 1]when you passXq,Yq, andZqas scalars.
Same as above Xq,Yq, andZqare vectors of the same size and orientation. Vector of same size and orientation asXq,Yq, andZq Ifsize(Xq) = [100 1],
andsize(Yq) = [100 1],
andsize(Zq) = [100 1],
thensize(Vq) = [100 1]
Same as above Xq,Yq, andZqare vectors of mixed orientation. size(Vq) = [length(Y) length(X) length(Z)] Ifsize(Xq) = [1 100],
andsize(Yq) = [50 1],
andsize(Zq) = [1 5],
thensize(Vq) = [50 100 5]
Same as above Xq,Yq, andZqare arrays of the same size. Array of the same size asXq,Yq, andZq Ifsize(Xq) = [50 25],
andsize(Yq) = [50 25],
andsize(Zq) = [50 25],
thensize(Vq) = [50 25]
interp3(V,k)
and variations of this syntax that includemethodorextrapval
None

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

Ifsize(V) = [10 12 5],
andk = 3,
thensize(Vq) = [73 89 33]

More About

collapse all

Strictly Monotonic

A set of values that are always increasing or 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 is no change in value betweenb(2)andb(3).The sequence,c = [2 4 6 8 6]contains a reversal betweenc(4)andc(5), so it is not monotonic at all.

Full Grid (in meshgrid Format)

Forinterp3, a full grid consists of three arrays whose elements represent a grid of points that define a region inR3.The first array contains thex-coordinates, the second array contains they-coordinates, and the third array contains thez-coordinates. The values in each array vary along a single dimension and are constant along the other dimensions.

的值in thex-array arestrictly monotonic, increasing, and vary along the second dimension. The values in they-array are strictly monotonic, increasing, and vary along the first dimension. The values in thez-array are strictly monotonic, increasing, and vary along the third dimension. Use themeshgridfunction to create a full grid that you can pass tointerp3

Grid Vectors

Forinterp3, grid vectors consist of three vectors of mixed-orientation that define the points on a grid inR3

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

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

Scattered Points

Forinterp3, scattered points consist of three arrays or vectors,Xq,Yq, andZq, that define a collection of points scattered inR3.第i个数组包含的坐标h 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

Introduced before R2006a