How to compute distances between centroids and data matrix (for kmeans algorithm)

前端 未结 2 952
一向
一向 2021-02-02 02:08

I am a student of clustering and R. In order to obtain a better grip of both I would like to compute the distance between centroids and my xy-matrix for each iteration till it \

2条回答
  •  青春惊慌失措
    2021-02-02 02:37

    Modified the distance matrix function above (added another loop for no. of points) as the above function displays only the distance of first point from all clusters and not all points, which is what the question is looking for:

    myEuclid <- function(points1, points2) {
        distanceMatrix <- matrix(NA, nrow=dim(points1)[1], ncol=dim(points2)[1])
        for(i in 1:nrow(points2)) {
            for (j in c(1:dim(t(points1))[2])) {
                
            distanceMatrix[j,i] <- sqrt(rowSums(t(t(points1)[,j]-t(points2[i,]))^2))
                }
        }
        distanceMatrix
    }

    Do let me know if this works fine!

提交回复
热议问题