R/distance.R

Defines functions distance

Documented in distance

distance <- function(m1, m2, type = "euclidean") {

  type <- match.arg(type, c("euclidean", "manhattan"))
  m1 <- as.matrix(m1)
  m2 <- as.matrix(m2)

  if (ncol(m1) < 2 || ncol(m2) < 2) {
    stop("'m1' and 'm2' must each have at least two coordinate columns")
  }

  k <- nrow(m1)
  l <- nrow(m2)
  d <- matrix(data = NA_real_, nrow = k, ncol = l)

  for (i in seq_len(k)) {
    if (type == "euclidean") {
      d[i, ] <- sqrt(((m1[i, 1] - m2[, 1])^2) + ((m1[i, 2] - m2[, 2])^2))
    } else {
      d[i, ] <- abs(m1[i, 1] - m2[, 1]) + abs(m1[i, 2] - m2[, 2])
    }
  }

  return(d)
}

Try the SpatialAcc package in your browser

Any scripts or data that you put into this service are public.

SpatialAcc documentation built on April 29, 2026, 1:08 a.m.