您还可以考虑Moran's I,它在 R 包“ape”中提供。然后简单地使用基于距离的加权:
nRows <- 30
nCols <- 15
nPixels <- nRows * nCols
# Create a Random Image
image <- matrix(sample.int(256, nPixels, replace=TRUE),
nrow=nRows, ncol=nCols) - 1L
# 1D to 2D Index Function
reverseIndex <- function ( vectorIdx, nRows, nCols )
{
# If you're using row major for some odd reason, you'll
# need to flip these.
J <- floor((vectorIdx - 1L) / nCols)
I <- (vectorIdx - 1L) - nCols*J
# Return:
c(I+1L, J+1L)
}
# Distance Function
distFunc <- function(I, J)
{
idx1 <- reverseIndex(I, nRows, nCols)
idx2 <- reverseIndex(J, nRows, nCols)
idDiff <- idx1 - idx2
# Return:
sqrt(idDiff %*% idDiff)
}
# Create Distance Matrix
matrix(mapply(distFunc,
rep(seq_len(nPixels), nPixels),
rep(seq_len(nPixels), each=nPixels)),
nrow=nPixels, ncol=nPixels)
# Invert Distance for Moran's I
invDist <- 1 / dist
diag(invDist) <- 0
# Compute Moran's I:
ape::Moran.I(as.vector(image), dist)
请注意,这将仅提供关联的度量和测试,它不会确定该关联在您的矩阵中的位置。