Learn R Programming

pracma (version 1.5.8)

interp2: 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(x)-by-length(y) matrix.
xp, yp
x-, y-coordinates of points at which interpolated values will be computed.
method
interpolation method, ``linear'' the most useful.

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.

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.

See Also

interp1, barylag2d

Examples

Run this code
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()

Run the code above in your browser using DataLab