如何计算R中向量的欧几里德范数? [英] how to calculate the Euclidean norm of a vector in R?

查看:45
本文介绍了如何计算R中向量的欧几里德范数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了 norm,但我认为它给出了错误的结果.(c(1, 2, 3)的范数是sqrt(1*1+2*​​2+3*3),但是返回6...

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..

x1 <- 1:3
norm(x1)
# Error in norm(x1) : 'A' must be a numeric matrix
norm(as.matrix(x1))
# [1] 6
as.matrix(x1)
#      [,1]
# [1,]    1
# [2,]    2
# [3,]    3
norm(as.matrix(x1))
# [1] 6

有谁知道 R 中计算向量范数的函数是什么?

Does anyone know what's the function to calculate the norm of a vector in R?

推荐答案

这个小函数自己写:

norm_vec <- function(x) sqrt(sum(x^2))

这篇关于如何计算R中向量的欧几里德范数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆