Two-dimensional Data Interpolation
Two-dimensional data interpolation similar to a table look-up.
interp2(x, y, Z, xp, yp, method = c("linear", "nearest", "constant"))
x, y |
vectors with monotonically increasing elements, representing
x- and y-coordinates of the data values in |
Z |
numeric |
xp, yp |
x-, y-coordinates of points at which interpolated values will be computed. |
method |
interpolation method, “linear” the most useful. |
Computes a vector containing elements corresponding to the elements of
xp
and yp
, determining by interpolation within the
two-dimensional function specified by vectors x
and y
,
and matrix Z
.
x
and y
must be monotonically increasing. They specify
the points at which the data Z
is given.
Therefore, length(x) = nrow(Z)
and length(y) = ncol(Z)
must be satisfied.
xp
and yp
must be of the same length.
The functions appears vectorized as xp
, yp
can be
vectors, but internally they are treated in a for
loop.
Vector the length of xp
of interpolated values.
For methods “constant” and “nearest” the intervals are considered closed from left and below. Out of range values are returned as NAs.
The corresponding Matlab function has also the methods “cubic” and
“spline”. If in need of a nonlinear interpolation, take a look at
barylag2d
in this package and the example therein.
interp1
, barylag2d
## Not run: x <- linspace(-1, 1, 11) y <- linspace(-1, 1, 11) mgrid <- meshgrid(x, y) Z <- mgrid$X^2 + mgrid$Y^2 xp <- yp <- linspace(-1, 1, 101) method <- "linear" zp <- interp2(x, y, Z, xp, yp, method) plot(xp, zp, type = "l", col = "blue") method = "nearest" zp <- interp2(x, y, Z, xp, yp, method) lines(xp, zp, col = "red") grid() ## End(Not run)
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.