Calculating all distances between one point and a group of points efficiently in R

后端 未结 5 621
太阳男子
太阳男子 2020-12-15 09:55

First of all, I am new to R (I started yesterday).

I have two groups of points, data and centers, the first one of size n and

5条回答
  •  心在旅途
    2020-12-15 10:31

    My solution:

    # data is a matrix where each row is a point
    # point is a vector of values
    euc.dist <- function(data, point) {
      apply(data, 1, function (row) sqrt(sum((point - row) ^ 2)))
    }
    

    You can try it, like:

    x <- matrix(rnorm(25), ncol=5)
    euc.dist(x, x[1,])
    

提交回复
热议问题