I get the error message "Error in dist.mat[j, i] <- d : number of items to replace is not a multiple of replacement length" when I run the script:
uncenter.distance <- function(X) {
n <- nrow(X)
dist.mat <- matrix(0, n, n)
xj <- X[1,]
for (i in 1:n) {
for (j in 1:n) {
yj <- X[j,]
d <- 1 - sum(xj %*% yj) / sqrt((xj)^2 * (yj)^2)
dist.mat[j,i] <- d
dist.mat[i,j] <- d
}
xj <- X[1+i,]
}
return(dist.mat)
}
sqrt((xj)^2 * (yj)^2)returns a vector of lengthn, thusdis also a vector of lengthn,dist.mat[j,i]expects a single value, that is whydist.mat[j,i] <- dcannot work. Did you forget to sum the part with the square root ? - fmarm