get_match: Extract closest points from regular 2d grid

View source: R/matchup_funcs.R

get_matchR Documentation

Extract closest points from regular 2d grid

Description

Given a 2d grid with corresponding x/y coordinates, and another x/y pair of vectors, find the data value closest to the second x/y vector pair. This is similar to pracma::interp2(), but chooses the nearest point instead of interpolating (see below for example).

Usage

get_match(grid_x, grid_y, grid_values, x, y)

Arguments

grid_x

Numeric vector of x-coordinates for the 2d grid (must match the number of columns in grid_values)

grid_y

Numeric vector of y-coordinates for the 2d grid (must match the number of rows in grid_values)

grid_values

Numeric matrix with number of columns/rows matchup grid_x and grid_y, respectively

x

Numeric vector of x-coordinates where you want to extract the nearest grid_values point (must be same length as y)

y

Numeric vector of y-coordinates where you want to extract the nearest grid_values point (must be same length as x)

Value

Numeric vector of grid values, same length as x and y

Examples

# This example is from the help file of pracma::interp2() for 2d interpolation.
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)
zp <- interp2(x, y, Z, xp, yp, "linear")

# Using get_match:
zp2 <- get_match(x, y, Z, xp, yp)

plot(zp, zp2)

BIO-RSG/oceancolouR documentation built on April 30, 2024, 7:54 a.m.