fixed.interpn
Interpolation for 1-D, 2-D, 3-D, and N-D gridded data in
ndgrid format
Since R2024a
Syntax
Description
returns interpolated values of a function of n variables at specific
query points using linear interpolation. The results always pass through the original
sampling of the function. Vq = fixed.interpn(X1,X2,...,Xn,V,Xq1,Xq2,...,Xqn)X1,X2,...,Xn contain the coordinates of the
sample points. V contains the corresponding function values at each
sample point. Xq1,Xq2,...,Xqn contain the coordinates of the query
points.
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 Vq = fixed.interpn(V,Xq1,Xq2,...,Xqn)V.
Use this syntax when you want to conserve memory and are not concerned about the absolute
distances between points.
Examples
This example shows how to implement an N-dimensional fixed-point lookup table using fixed.interpn.
Run the example multiple times to see the approximation over different query points.
Create Lookup Table for Function
Define a function f(x,y) to replace with a lookup table approximation.
clearvars f = @(x,y) sin(x)+sin(y)-(x.^2+y.^2)/20;
Define breakpoints x and y for the lookup table. Note that m and n do not have to be equal and x and y do not have to be linearly spaced.
m = 16; n = 16; x = linspace(-5,5,n); y = linspace(-5,5,m); [X,Y] = ndgrid(x,y);
Generate lookup table values V, corresponding to the breakpoints.
V = f(X,Y);
Query Lookup Table
Choose a random query point (xq,yq) in the ranges of x and y.
xq = fixed.example.realUniformRandomArray(x(1),x(end),1); yq = fixed.example.realUniformRandomArray(y(1),y(end),1);
Cast the inputs to 16-bit fixed-point.
x = fi(x); y = fi(y); V = fi(V); xq = fi(xq); yq = fi(yq);
The fixed.interpn function computes vq, the lookup table approximation of f(xq,yq).
vq = fixed.interpn(x,y,V,xq,yq)
vq =
-2.0808
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 16
FractionLength: 12
Compare Lookup Approximation to Actual Function Value
Compare vq to the actual function evaluation f(xq,yq).
vq_expected = f(double(xq),double(yq))
vq_expected = -2.1175
err = double(vq) - vq_expected
err = 0.0367
Plot f(x,y).
clf mesh(x,y,V,'EdgeAlpha',0.8,'FaceAlpha',0); xlabel('x') ylabel('y') zlabel('v') hold on
Plot vq, the lookup table approximation of f(xq,vq), using a red stem plot.
stem3(xq,yq,vq,'Filled','red') legend('f(x)','vq = Fixed-point lookup-table approximation of f(xq)','location','best')

Input Arguments
Sample grid points, specified as real arrays or vectors. The sample grid points must be strictly monotonically increasing in each dimension.
If
X1,X2,...,Xnare arrays, then they contain the coordinates of a full grid (inndgridformat). Use thengridfunction to create theX1,X2,...,Xnarrays together. These arrays must be the same size.If
X1,X2,...,Xnare vectors, then they are treated as grid vectors. The values in these vectors must be strictly monotonically increasing.
The inputs [X1,X2,...,Xn], V, and
[Xq1,Xq2,...,Xqn] must be the same data type:
fi, half, single, or
double. When using fi data, you can use the
shortened function name interpn.
Example: [X1,X2,X3,X4] =
fi(ndgrid(1:30,-10:10,1:5,10:13))
Example: [X1,X2,X3,X4] =
half(ndgrid(1:30,-10:10,1:5,10:13))
Data Types: fi | single | double
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
Valso hasndimensions, then the size ofVmust match the size of the n-dimensional grid defined byX1,X2,...,Xn. In this case,Vcontains one set of sample values at the sample points. For example, ifX1,X2,X3are 3-by-3-by-3 arrays, thenVcan also be a 3-by-3-by-3 array.If
Vhas more thanndimensions, then the firstndimensions ofVmust match the size of the n-dimensional grid defined byX1,X2,...,Xn. The extra dimensions inVdefine extra sets of sample values at the sample points. For example, ifX1,X2,X3are 3-by-3-by-3 arrays, thenVcan be a 3-by-3-by-3-by-2 array that defines two sets of sample values at the sample points.
If V contains complex numbers, then
fixed.interpn interpolates the real and imaginary parts
separately.
The inputs [X1,X2,...,Xn], V, and
[Xq1,Xq2,...,Xqn] must be the same data type:
fi, half, single, or
double. When using fi data, you can use the
shortened function name interpn.
Example: fi(rand(10,5,3,2))
Example: half(rand(10,5,3,2))
Data Types: fi | single | double
Complex Number Support: Yes
Query points, specified as real scalars, vectors, or arrays.
If
Xq1,Xq2,...,Xqnare scalars, then they are the coordinates of a single query point in Rn.If
Xq1,Xq2,...,Xqnare vectors of different orientations, thenXq1,Xq2,...,Xqnare treated as grid vectors in Rn.If
Xq1,Xq2,...,Xqnare vectors of the same size and orientation, thenXq1,Xq2,...,Xqnare treated as scattered points in Rn.If
Xq1,Xq2,...,Xqnare arrays of the same size, then they represent either a full grid of query points (inndgridformat) or scattered points in Rn.
The inputs [X1,X2,...,Xn], V, and
[Xq1,Xq2,...,Xqn] must be the same data type:
fi, half, single, or
double. When using fi data, you can use the
shortened function name interpn.
Example: [X1,X2,X3,X4] =
fi(ndgrid(1:10,1:5,7:9,10:11))
Example: [X1,X2,X3,X4] =
half(ndgrid(1:10,1:5,7:9,10:11))
Data Types: fi | single | double
Interpolation method, 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 method is the default interpolation method. | C0 |
|
"nearest" | The interpolated value at a query point is the value at the nearest sample grid point. | Discontinuous |
|
Function value outside the domain of X1,X2,...,Xn, specified as a
real or complex scalar. interpn returns this constant value for all
points outside the domain of X1,X2,...,Xn. If the scalar value is
nonzero and outside the range of the sample values v, then this value
is set to the minimum or maximum value of v, whichever is
closer.
The data type of extrapval must be the same as
[X1,X2,...,Xn], V, and
[Xq1,Xq2,...,Xqn].
The default behavior with fi input data is to return
0 for query points outside the domain. The default behavior with
half, single, or double input
data is to return NaN for query points outside the domain.
Example: fi(5)
Example: half(5+1i)
Data Types: fi | single | double
Complex Number Support: Yes
Note
The default behavior of the interpn function is to return
NaN when a query point is outside the domain. The
fixed.interpn function with fi input data is not
consistent with this behavior because fi casts NaN
to 0.
Output Arguments
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. The data type of Vq
is the same as that of the sample values V.
If you specify sample points with
X1,X2,...,Xn, or use the default grid, andVhas the same number of dimensions as the n-dimensional grid of sample points, thenVqcontains a single set of interpolated values at the query points defined byXq1,Xq2,...,Xqn.If
Xq1,Xq2,...,Xqnare scalars, thenVqis a scalar.If
Xq1,Xq2,...,Xqnare vectors of the same size and orientation, thenVqis a vector with the same size and orientation.If
Xq1,Xq2,...,Xqnare grid vectors of mixed orientation, thenVqis an array with the same size as the grid implicitly defined by the grid vectors.If
Xq1,Xq2,...,Xqnare arrays of the same size, thenVqis an array with the same size.
If you specify sample points with
X1,X2,...,Xn, or use the default grid, andVhas more dimensions than the n-dimensional grid of sample points, thenVqcontains multiple sets of interpolated values at the query points defined byXq1,Xq2,...,Xqn. In this case, the first n dimensions ofVqfollow the size rules for a single set of interpolated values above, butVqalso has the same extra dimensions asVwith the same sizes.With the syntax
fixed.interpn(V),Vqis an array with the same number of dimensions asV, where the size of the ith dimension is2 * (size(V,i)-1)+1.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
HDL Code Generation
Generate VHDL, Verilog and SystemVerilog code for FPGA and ASIC designs using HDL Coder™.
Version History
Introduced in R2024a
See Also
fixed.interp1 | fixed.interp2 | fixed.interp3 | ndgrid | interpn
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.
Seleziona un sito web
Seleziona un sito web per visualizzare contenuto tradotto dove disponibile e vedere eventi e offerte locali. In base alla tua area geografica, ti consigliamo di selezionare: .
Puoi anche selezionare un sito web dal seguente elenco:
Come ottenere le migliori prestazioni del sito
Per ottenere le migliori prestazioni del sito, seleziona il sito cinese (in cinese o in inglese). I siti MathWorks per gli altri paesi non sono ottimizzati per essere visitati dalla tua area geografica.
Americhe
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- 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)