R/distMat.R

Defines functions corMat distMat

Documented in distMat

#' Distance matrices
#' @aliases corMat, distMat
#'
#' `distMat` returns `stats::dist(X, diag=TRUE, upper=TRUE)`. `corMat` returns a distance matrix
#' from the correlation matrix.
#' 
#' @param X A matrix
#' @param remove.mean remove the mean from each row of X
#' @keywords clustering
#' @export
distMat <- function(X, remove.mean=FALSE){
  K <- nrow(X)
  if(remove.mean==TRUE) X <- t(scale(t(X), scale=FALSE))
  distanceMatrix <- matrix(NA, nrow=K, ncol=K)
  for(r in 1:K){
    for(c in 1:K){
      distanceMatrix[r,c] <- sqrt(sum((X[r,]-X[c,])^2))
    }
  }
  distanceMatrix
}

corMat <- function(X){
  K <- nrow(X)
  distanceMatrix <- matrix(NA, nrow=K, ncol=K)
  for(r in 1:K){
    for(c in 1:K){
      distanceMatrix[r,c] <- stats::cor(X[r,], X[c,])
    }
  }
  distanceMatrix
}
UW-Upwelling-Project/imageryML documentation built on Dec. 18, 2021, 6:11 p.m.