Learn R Programming

beadplexr (version 0.5.0)

count_neighbours: Count neighbours for each element in a binary matrix

Description

Count neighbours for each element in a binary matrix

Usage

count_neighbours(x)

Value

A matrix of the same size as x giving the number of elements with non-zero values in neighbouring cells of the matrix.

Arguments

x

The matrix to count the neighbours in.

Examples

Run this code

x <- matrix(
c(1L, 1L, 0L,
  1L, 1L, 0L,
  0L, 1L, 1L),
nrow = 3,
ncol = 3,
byrow = TRUE
)
beadplexr:::count_neighbours(x)

x <- matrix(
  c(TRUE, TRUE, FALSE,
    TRUE, TRUE, FALSE,
   FALSE, TRUE, TRUE),
  nrow = 3,
  ncol = 3,
 byrow = TRUE
)
beadplexr:::count_neighbours(x)

x <- matrix(
  c(1, 1, 0, 0,
    1, 0, 0, 0,
    0, 1, 1 ,1,
    0, 1, 1, 1),
  nrow = 4,
  ncol = 4,
  byrow = TRUE
)
beadplexr:::count_neighbours(x)

if (FALSE) {
  x <- matrix(
    c("1", "1", "0",
      "1", "1", "0",
      "0", "1", "1"),
    nrow = 3,
    ncol = 3,
    byrow = TRUE
  )
  #beadplexr:::count_neighbours(x)
}

Run the code above in your browser using DataLab