interp3
Interpolation for 3-D gridded data in meshgrid format
Syntax
Description
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.Vq
= interp3(X,Y,Z
,V
,Xq,Yq,Zq
)X
,Y
, andZ
contain the coordinates of the sample points.V
contains the corresponding function values at each sample point.Xq
,Yq
, andZq
contain the coordinates of the query points.
also specifiesVq
= interp3(___,method
,extrapval
)extrapval
, a scalar value that is assigned to all queries that lie outside the domain of the sample points.
If you omit theextrapval
argument for queries outside the domain of the sample points, then based on themethod
argumentinterp3
returns one of the following:
The extrapolated values for the
'spline'
and'makima'
methodsNaN
values for other interpolation methods
Examples
Interpolate Using Default Method
Load the points and values of the flow function, sampled at 10 points in each dimension.
[X,Y,Z,V] = flow(10);
Theflow
function returns the grid in the arrays,X
,Y
,Z
.The grid covers the region,
,
,
, and the spacing is
,
, and
.
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
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
Interpolate Using Cubic Method
Load the points and values of the flow function, sampled at 10 points in each dimension.
[X,Y,Z,V] = flow(10);
Theflow
function returns the grid in the arrays,X
,Y
,Z
.The grid covers the region,
,
,
, and the spacing is
,
, and
.
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
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
Evaluate Outside the Domain of X, Y, and Z
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 therand
function to create the array.
rng('default') V = rand(50,100,30);
EvaluateV
at 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-1
because they are outside the domain ofx
,y
, andz
.
输入参数
X,Y,Z
—Sample grid points
arrays|vectors
Sample grid points, specified as real arrays or vectors. The sample grid points must be unique.
If
X
,Y
, andZ
are arrays, then they contain the coordinates of afull grid (in meshgrid format).Use themeshgrid
function to create theX
,Y
, andZ
arrays together. These arrays must be the same size.If
X
,Y
, andZ
are 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
V
—Sample values
array
Sample values, specified as a real or complex array. The size requirements forV
depend on the size ofX
,Y
, andZ
:
If
X
,Y
, andZ
are arrays representing a full grid (inmeshgrid
format), then the size ofV
matches the size ofX
,Y
, orZ
.If
X
,Y
, andZ
are grid vectors, thensize(V) = [length(Y) length(X) length(Z)]
.
IfV
contains complex numbers, theninterp3
interpolates the real and imaginary parts separately.
Example:rand(10,10,10)
Data Types:single
|double
Complex Number Support:Yes
Xq,Yq,Zq
—Query points
scalars|vectors|arrays
Query points, specified as a real scalars, vectors, or arrays.
If
Xq
,Yq
, andZq
are scalars, then they are the coordinates of a single query point inR3.If
Xq
,Yq
, andZq
are vectors of different orientations, thenXq
,Yq
, andZq
are treated as grid vectors inR3.If
Xq
,Yq
, andZq
are vectors of the same size and orientation, thenXq
,Yq
, andZq
are treated asscattered pointsinR3.If
Xq
,Yq
, andZq
are arrays of the same size, then they represent either a full grid of query points (inmeshgrid
format) or scattered points inR3.
Example:[Xq,Yq,Zq] = meshgrid((1:0.1:10),(-5:0.1:0),3:5)
Data Types:single
|double
k
—Refinement factor
1
(default) |real, nonnegative, integer scalar
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-1
interpolated points between sample values.
Ifk
is0
, thenVq
is the same asV
.
interp3(V,1)
is the same asinterp3(V)
.
The following illustration depictsk=2
in one plane ofR3.There are 72 interpolated values in red and 9 sample values in black.
Example:interp3(V,2)
Data Types:single
|double
method
—插值法
'linear'
(default) |'nearest'
|'cubic'
|'spline'
|'makima'
插值法, 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 |
|
'nearest' |
The interpolated value at a query point is the value at the nearest sample grid point. | Discontinuous |
|
'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 |
|
'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 |
|
'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 |
|
extrapval
—Function value outside domain ofX
,Y
, andZ
scalar
Function value outside domain ofX
,Y
, andZ
, specified as a real or complex scalar.interp3
returns 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
Vq
— Interpolated values
scalar | vector | array
Interpolated values, returned as a real or complex scalar, vector, or array. The size and shape ofVq
depends 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 include method orextrapval |
Xq ,Yq , andZq are scalars. |
Scalar | size(Vq) = [1 1] when you passXq ,Yq , andZq as scalars. |
Same as above | Xq ,Yq , andZq are vectors of the same size and orientation. |
Vector of same size and orientation asXq ,Yq , andZq |
Ifsize(Xq) = [100 1] ,and size(Yq) = [100 1] ,and size(Zq) = [100 1] ,then size(Vq) = [100 1] . |
Same as above | Xq ,Yq , andZq are vectors of mixed orientation. |
size(Vq) = [length(Y) length(X) length(Z)] |
Ifsize(Xq) = [1 100] ,and size(Yq) = [50 1] ,and size(Zq) = [1 5] ,then size(Vq) = [50 100 5] . |
Same as above | Xq ,Yq , andZq are arrays of the same size. |
Array of the same size asXq ,Yq , andZq |
Ifsize(Xq) = [50 25] ,and size(Yq) = [50 25] ,and size(Zq) = [50 25] ,then size(Vq) = [50 25] . |
interp3(V,k) and variations of this syntax that include method orextrapval |
None | Array in which the length of the |
Ifsize(V) = [10 12 5] ,and k = 3 ,then size(Vq) = [73 89 33] . |
More About
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 themeshgrid
function 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
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
Xq
,Yq
, andZq
must be the same size. Usemeshgrid
to evaluate on a grid.For best results, provide
X
,Y
, andZ
as vectors. The values in these vectors must be strictly monotonic and increasing.Code generation does not support the
'makima'
interpolation method.For the
'cubic'
interpolation method, if the grid does not have uniform spacing, an error results. In this case, use the'spline'
interpolation method.For best results when you use the
'spline'
interpolation method:Use
meshgrid
to create the inputsXq
,Yq
, andZq
.Use a small number of interpolation points relative to the dimensions of
V
.Interpolating over a large set of scattered points can be inefficient.
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, seeRun MATLAB Functions in Thread-Based Environment.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
Usage notes and limitations:
V
must be a double or single 3-D array.V
can be real or complex.X
,Y
, andZ
must:Have the same type (double or single).
Be finite vectors or 3-D arrays with increasing and nonrepeating elements in corresponding dimensions.
Align with Cartesian axes when
X
,Y
, andZ
are 3-D arrays (as if they were produced bymeshgrid
).Have dimensions consistent with
V
.
Xq
,Yq
, andZq
must be vectors or arrays of the same type (double or single). IfXq
,Yq
, andZq
are arrays, then they must have the same size. If they are vectors with different lengths, then one of them must have a different orientation.method
must be'linear'
or'nearest'
.The extrapolation for the out-of-boundary input is not supported.
For more information, seeRun MATLAB Functions on a GPU(Parallel Computing Toolbox).
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
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:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
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
- United Kingdom(English)