interp2

interp2 returns interpolated data of a two-dimensional function with variables x and y at specific query points (xq,yq).
  • interp2(x,y,v,xq,yq)
    • x,y: coordinates of the sample points; x and y can be vectors or matrices. If vectors, x and y are treated as vectors that define the x- and y-coordinates in a grid. They must be strictly monotonic and increasing in values. If matrices, x and y must define a full grid in meshgrid format
    • v: evaluation of the two-dimensional function at sample points (x,y). If x and y are (grid) vectors, v must be a matrix with a number of rows equal to the length of y and a number of columns equal to the length of x. If x and y are matrices, v must be a matrix with the same size of x and y
    • xq,yq: coordinates of querying points. If xq and yq are scalars, they are the coordinates of a single point. If xq and yq are vectors with different orientation they are treated as vectors defining a grid. If xq and yq are vectors with same size and orientation they are treated as scattered points. If xq and yq are matrices they represent either a full grid or a matrix of scattered points
  • interp2(x,y,v,xq,yq,method)
  • method can be:
    • linear (default): the interpolated values are the result of linear interpolation between neighbouring values of v in both dimensions
    • cubic: the interpolated values are the result of cubic convolution between neighbouring values of v in both dimensions
    • nearest: the interpolated values are the nearest value of v
    • spline: the interpolated values are the result of cubic spline interpolation between neighbouring values of v using not-a-knot end conditions
  • interp2(x,y,v,xq,yq,method,extrapval)
    • extrapval: it specifies the scalar that is assigned to all queries laying outside the domain defined by the sample coordinates (x,y). If not specified, this value is set to NaN

Examples

>> [x,y] = meshgrid(1:2:10) 

% coordinates of sample points

% sample points

v = sin(x).*sin(y)

% query coordinates defining a finer grid

[xq,yq] = meshgrid(1:0.25:10)

% linear interpolation

vq = interp2(x,y,v,xq,yq)

% plot the linear interpolation

o = struct('show', false) op = struct('title', 'Linear interpolation') s = scatter3(x,y,v,o)

% original sampling shown in dots

overlay({surf(xq,yq,vq,o),s},op)
>> [x,y] = meshgrid(1:2:10) 

% coordinates of sample points

% sample points

v = sin(x).*sin(y)

% query coordinates defining a finer grid

[xq,yq] = meshgrid(1:0.25:10)

% cubic interpolation

vq = interp2(x,y,v,xq,yq,'cubic')

% plot the cubic interpolation

o = struct('show', false) op = struct('title', 'Cubic interpolation') s = scatter3(x,y,v,o)

% original sampling shown in dots

overlay({surf(xq,yq,vq,o),s},op)
>> [x,y] = meshgrid(1:2:10) 

% coordinates of sample points

% sample points

v = sin(x).*sin(y)

% query coordinates defining a finer grid % vector format

xq = 1:0.25:10 yq = [1:0.25:10]'

% nearest interpolation

vq = interp2(x,y,v,xq,yq,'nearest')

% plot the nearest interpolation

[xq,yq] = meshgrid(xq,yq) o = struct('show', false) op = struct('title', 'Nearest interpolation') s = scatter3(x,y,v,o)

% original sampling shown in dots

overlay({surf(xq,yq,vq,o),s},op)
>> [x,y] = meshgrid(1:2:10) % coordinates of sample points

% sample points

v = sin(x).*sin(y)

% query coordinates defining a finer grid % vectors with different directions

xq = [1:0.25:10]' yq = 1:0.25:10

% spline interpolation

vq = interp2(x,y,v,xq,yq,'spline')

% plot the spline interpolation

[xq,yq] = meshgrid(xq,yq) o = struct('show', false) op = struct('title', 'Spline interpolation') s = scatter3(x,y,v,o)

% original sampling shown in dots

overlay({surf(xq,yq,vq,o),s},op)

See also

interp1 | spline | pchip