how to calculate the Euclidean norm of a vector in R?

后端 未结 10 1096
灰色年华
灰色年华 2021-02-01 13:22

I tried norm, but I think it gives the wrong result. (the norm of c(1, 2, 3) is sqrt(1*1+2*2+3*3), but it returns 6..

10条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-01 13:58

    I'mma throw this out there too as an equivalent R expression

    norm_vec(x) <- function(x){sqrt(crossprod(x))}
    

    Don't confuse R's crossprod with a similarly named vector/cross product. That naming is known to cause confusion especially for those with a physics/mechanics background.

提交回复
热议问题