how to calculate the Euclidean norm of a vector in R? -
i tried norm
, think gives wrong result. (the norm of c(1, 2, 3)
sqrt(1*1+2*2+3*3)
, returns 6
..
x1 <- 1:3 norm(x1) # error in norm(x1) : 'a' must numeric matrix norm(as.matrix(x1)) # [1] 6 as.matrix(x1) # [,1] # [1,] 1 # [2,] 2 # [3,] 3 norm(as.matrix(x1)) # [1] 6
does know what's function calculate norm of vector in r?
this trivial function write yourself:
norm_vec <- function(x) sqrt(sum(x^2))
Comments
Post a Comment