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

后端 未结 10 1098
灰色年华
灰色年华 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 14:09

    norm(x, type = c("O", "I", "F", "M", "2"))
    

    The default is "O".

    "O", "o" or "1" specifies the one norm, (maximum absolute column sum);

    "F" or "f" specifies the Frobenius norm (the Euclidean norm of x treated as if it were a vector);

    norm(as.matrix(x1),"o")
    

    The result is 6, same as norm(as.matrix(x1))

    norm(as.matrix(x1),"f")
    

    The result is sqrt(1*1+2*2+3*3)

    So, norm(as.matrix(x1),"f") is answer.

提交回复
热议问题