matrix_get_neighbors: Get neighboring cell values for given matrix cell

Description Usage Arguments Value Examples

View source: R/matrix_get_neighbors.R

Description

Get the values of a k*k neighborhood, as vector and by row, given a matrix, k, and focal cell position (row and column).

Usage

1
matrix_get_neighbors(m, pos, k = 3)

Arguments

m

A matrix.

pos

The focal cell position, a numeric vector of length two of the form c(row, column).

k

Neighborhood size around the focal cell. For example, k=3 implies a neighborhood of size 3*3. Must be an odd positive integer.

Value

A vector with cell values, ordered by rows, starting from the top left corner of the neighborhood and to the right. When neighborhood extends beyond matrix bounds, only the "existing" values are returned.

Examples

1
2
3
4
5
m = matrix(1:12, nrow = 3, ncol = 4)
m
matrix_get_neighbors(m, pos = c(2, 2), k = 3)
matrix_get_neighbors(m, pos = c(2, 2), k = 5)
matrix_get_neighbors(m, pos = c(2, 2), k = 7)  # Same result

starsExtra documentation built on Nov. 18, 2021, 5:08 p.m.