interp2: Two-dimensional Data Interpolation

View source: R/interp2.R

interp2R Documentation

Two-dimensional Data Interpolation

Description

Two-dimensional data interpolation similar to a table look-up.

Usage

    interp2(x, y, Z, xp, yp, method = c("linear", "nearest", "constant"))

Arguments

x, y

vectors with monotonically increasing elements, representing x- and y-coordinates of the data values in Z.

Z

numeric length(y)-by-length(x) matrix.

xp, yp

x-, y-coordinates of points at which interpolated values will be computed.

method

interpolation method, “linear” the most useful.

Details

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.

Value

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.

Note

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.

See Also

interp1, barylag2d

Examples

## 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)

pracma documentation built on Nov. 10, 2023, 1:14 a.m.