Euclidean distance of two vectors

后端 未结 4 1640
南旧
南旧 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:35

    Use the dist() function, but you need to form a matrix from the two inputs for the first argument to dist():

    dist(rbind(x1, x2))
    

    For the input in the OP's question we get:

    > dist(rbind(x1, x2))
            x1
    x2 7.94821
    

    a single value that is the Euclidean distance between x1 and x2.

提交回复
热议问题