Euclidean distance of two vectors

后端 未结 4 1642
南旧
南旧 2020-11-29 02:32

How do I find the Euclidean distance of two vectors:

x1 <- rnorm(30)
x2 <- rnorm(30)
4条回答
  •  半阙折子戏
    2020-11-29 03:21

    As defined on Wikipedia, this should do it.

    euc.dist <- function(x1, x2) sqrt(sum((x1 - x2) ^ 2))
    

    There's also the rdist function in the fields package that may be useful. See here.


    EDIT: Changed ** operator to ^. Thanks, Gavin.

提交回复
热议问题