Return the 8 neighbors, as cell numbers, of each cell on a raster.

ngb8(n_row, n_col)

Arguments

n_row

Integer. The number of rows of a Raster or object.

n_col

Integer. The number of columns of a Raster object.

Value

Named list, the nth element of the list corresponds to the 8 adjacent cell numbers of the nth cell on the Raster* object.

Details

A cell with coordinates (x, y) has 8 neighbors with coordinates: (x±1, y), (x, y±1) and (x±1, y±1). Cells on the edge of a raster have less than 8 neighbors. The function identifies the neighbors of a cell as cell numbers.

See also

Examples

## Matrix m mocking a raster of 3 rows and 4 columns
m <- matrix(1:12, nrow = 3, ncol = 4, byrow = TRUE)
m
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    2    3    4
#> [2,]    5    6    7    8
#> [3,]    9   10   11   12

nbs <- ngb8(3, 4)
nbs
#> $`1`
#> [1] 2 5 6
#> 
#> $`2`
#> [1] 1 3 5 6 7
#> 
#> $`3`
#> [1] 2 4 6 7 8
#> 
#> $`4`
#> [1] 3 7 8
#> 
#> $`5`
#> [1]  1  2  6  9 10
#> 
#> $`6`
#> [1]  1  2  3  5  7  9 10 11
#> 
#> $`7`
#> [1]  2  3  4  6  8 10 11 12
#> 
#> $`8`
#> [1]  3  4  7 11 12
#> 
#> $`9`
#> [1]  5  6 10
#> 
#> $`10`
#> [1]  5  6  7  9 11
#> 
#> $`11`
#> [1]  6  7  8 10 12
#> 
#> $`12`
#> [1]  7  8 11
#>