interp2
Builtin index · Signal processing
result = interp2(x, y, Z, xq, yq, [method="bilinear"|"nearest"])
Input: x, y, Z, xq, yq, method · Output: result
What it is
x: a length-ncols vector, the grid's COLUMN coordinates (strictly increasing); y: a length-nrows vector, the grid's ROW coordinates (strictly increasing); Z: an (nrows, ncols) matrix with Z[i,j] the value at (y[i], x[j]) — Matrix::get(row, col)'s own indexing convention used everywhere else in this codebase, not a swapped axis order; xq/yq: paired query coordinates, either both numbers or both length-M vectors (xq[k]/yq[k] is query point k — not an implied meshgrid over every combination); method (optional string: "bilinear" default or "nearest"). 2-D grid interpolation (MATLAB's interp2): a query exactly at a grid coordinate returns that exact stored value, not an approximation; extrapolation beyond the grid is always on, matching interp1.
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = interp2(x, y, Z, xq, yq, [method="bilinear"|"nearest"]) | x vectorx: a length-ncols vector, the grid's COLUMN coordinates (strictly increasing).y vectory: a length-nrows vector, the grid's ROW coordinates (strictly increasing).Z matrixZ: an (nrows, ncols) matrix with Z[i,j] the value at (y[i], x[j]) — Matrix::get(row, col)'s own indexing convention used everywhere else in this codebase, not a swapped axis order.xq number or vectorxq/yq: paired query coordinates, either both numbers or both length-M vectors (xq[k]/yq[k] is query point k — not an implied meshgrid over every combination).yq number or vectorxq/yq: paired query coordinates, either both numbers or both length-M vectors (xq[k]/yq[k] is query point k — not an implied meshgrid over every combination).method string, optional (default "bilinear"|"nearest")method (optional string: "bilinear" default or "nearest"). | result number or vector | Returns a number (if xq/yq are numbers) or a length-M vector (if they are vectors). |
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
x = [0, 1, 2] # column coordinates
y = [0, 1] # row coordinates
Z = [1, 2, 3; 4, 5, 6] # Z[i,j] at (y[i], x[j])
v_grid = interp2(x, y, Z, 1, 1) # exact grid point, no interpolation error
v_center = interp2(x, y, Z, 0.5, 0.5) # bilinear average of the 4 surrounding corners
print("interp2 at grid point (x=1,y=1): {v_grid}")
print("interp2 at center (x=0.5,y=0.5): {v_center}")
interp2 at grid point (x=1,y=1): 5 interp2 at center (x=0.5,y=0.5): 3
from signal-processing
How it works
interp2(x, y, Z, xq, yq, [method]) — 2D grid interpolation (MATLAB's interp2). x (length ncols) are the grid's COLUMN coordinates and y (length nrows) its ROW coordinates; Z is an (nrows, ncols) matrix with Z[i,j] the value at (y[i], x[j]) — Matrix::get(row, col)'s own indexing convention used everywhere else in this codebase (heatmap, matrix_row, ...), NOT a transposed/swapped axis order. x/y are each assumed strictly increasing (the same convention interp1's x already assumes, per its own doc comment). xq/yq are paired query coordinates — either both scalars (one point) or both vectors of the SAME length (xq[k]/yq[k] is query point k, MATLAB's convention — not an implied meshgrid over every combination of the two). method is "bilinear" (default) or "nearest"; extrapolation beyond the grid is always on, matching interp1. Exactness at grid points: interp_grid_bracket's own doc comment proves each axis's fractional position t comes out as EXACTLY 0.0 or 1.0 (not just close) whenever the query coordinate exactly equals one of that axis's own grid coordinates, floating-point division of identical nonzero values being exact in IEEE 754 — so a query at (x[j], y[i]) collapses the bilinear blend to Z[i,j] with no rounding error at all, verified directly in the acceptance tests rather than just assumed from the algebra.
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.