fixed.interp2
Syntax
Description
returns interpolated values of a function of two variables at specific query points using
linear interpolation. The results always pass through the original sampling of the
function. Vq
= fixed.interp2(X,Y
,V
,Xq,Yq
)X
and Y
contain the coordinates of the
sample points. V
contains the corresponding function values at each
sample point. Xq
and Yq
contain the coordinates of
the query points.
Examples
Implement 2D FixedPoint Lookup Tables Using Interpolation
This example shows how to implement a twodimensional lookup table using fixed.interp2
.
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] = meshgrid(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 16bit fixedpoint.
x = fi(x); y = fi(y); V = fi(V); xq = fi(xq); yq = fi(yq);
The fixed.interp2
function computes vq
, the lookup table approximation of f(xq,yq)
.
vq = fixed.interp2(x,y,V,xq,yq)
vq = 2.0808 DataTypeMode: Fixedpoint: 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 = Fixedpoint lookuptable approximation of f(xq)','location','best')
Input Arguments
X,Y
— Sample grid points
matrices  vectors
Sample grid points, specified as real matrices or vectors. The sample grid points must be strictly monotonically increasing in both dimensions.
If
X
andY
are matrices, then they contain the coordinates of a full grid (inmeshgrid
format). Use themeshgrid
function to create theX
andY
matrices together. Both matrices must be the same size.If
X
andY
are vectors, then they are treated as grid vectors. The values in both vectors must be strictly monotonically increasing.
The inputs [X,Y]
, V
, and
[Xq,Yq]
must be the same data type: fi
,
half
, single
, or double
. When
using fi
data, you can use the shortened function name
interp2
.
Example: [X,Y] = fi(meshgrid(1:30,10:10),0,12,8)
Example: [X,Y] = half(meshgrid(1:30,10:10))
Data Types: fi
 single
 double
V
— Sample values
matrix
Sample values, specified as a real or complex matrix. The size requirements for
V
depend on the size of X
and
Y
:
If
X
andY
are matrices representing a full grid (inmeshgrid
format), thenV
must be the same size asX
andY
.If
X
andY
are grid vectors, thenV
must be a matrix containinglength(Y)
rows andlength(X)
columns.
If V
contains complex numbers, then
fixed.interp2
interpolates the real and imaginary parts
separately.
The inputs [X,Y]
, V
, and
[Xq,Yq]
must be the same data type: fi
,
half
, single
, or double
. When
using fi
data, you can use the shortened function name
interp2
.
Example: fi(rand(10))
Example: half(rand(10))
Data Types: fi
 single
 double
Complex Number Support: Yes
Xq,Yq
— Query points
scalars  vectors  matrices  arrays
Query points, specified as a real scalars, vectors, matrices, or arrays.
If
Xq
andYq
are scalars, then they are the coordinates of a single query point.If
Xq
andYq
are vectors of different orientations, thenXq
andYq
are treated as grid vectors.If
Xq
andYq
are vectors of the same size and orientation, thenXq
andYq
are treated as scattered points in 2D space.If
Xq
andYq
are matrices, then they represent either a full grid of query points (inmeshgrid
format) or scattered points.If
Xq
andYq
are ND arrays, then they represent scattered points in 2D space.
The inputs [X,Y]
, V
, and
[Xq,Yq]
must be the same data type: fi
,
half
, single
, or double
. When
using fi
data, you can use the shortened function name
interp2
.
Example: [Xq,Yq] =
fi(meshgrid((1:0.1:10),(5:0.1:0)))
Data Types: fi
 single
 double
method
— Interpolation method
"linear"
(default)  "nearest"
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.  C^{0} 

"nearest"  The interpolated value at a query point is the value at the nearest sample grid point.  Discontinuous 

extrapval
— Function value outside domain of X
and Y
scalar
Function value outside the domain of X
and Y
,
specified as a real or complex scalar. fixed.interp2
returns this
constant value for all points outside the domain of X
and
Y
. If the scalar value is nonzero and outside the range of the
sample values V
, then the value is set to the minimum or maximum
value of V
, whichever is closer.
The data type of extrapval
must be the same as
[X,Y]
, V
, and [Xq,Yq]
.
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 interp2
function is to return
NaN
when a query point is outside the domain. The
fixed.interp2
function with fi
input data is not
consistent with this behavior because fi
casts NaN
to 0
.
Output Arguments
Vq
— Interpolated values
scalar  vector  matrix
Interpolated values, returned as a real or complex scalar, vector, or matrix. 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
.
Syntaxes  Special Conditions  Size of Vq  Example 

fixed.interp2(X,Y,V,Xq,Yq) ,fixed.interp2(V,Xq,Yq) ,and variations of these syntaxes that include method or
extrapval  Xq and Yq are scalars  Scalar  size(Vq) = [1 1] when you pass Xq
and Yq as scalars. 
Same as above  Xq and Yq are vectors of the same
size and orientation  Vector of same size and orientation as Xq and
Yq  If size(Xq) = [100 1] and size(Yq) = [100 1] , then size(Vq) = [100 1] . 
Same as above  Xq and Yq are vectors of mixed
orientation  Matrix in which the number of rows is length(Yq) , and
the number of columns is length(Xq)  If size(Xq) = [1 100] and size(Yq) = [50 1] , then size(Vq) = [50 100] . 
Same as above  Xq and Yq are matrices or arrays of
the same size  Matrix or array of the same size as Xq and
Yq  If size(Xq) = [50 25] and size(Yq) = [50 25] , then size(Vq) = [50 25] . 
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.interp3
 fixed.interpn
 meshgrid
 interp2
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)