Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

L1 and L2 norms in R

Tags:

r

I have a vector e <- c(0.1, -0.1, 0.1) and I want to calculate L1 and L2 norms. I am using norm(e, type="2") which works fine for L2 norm but when I change it to norm(e, type="1") or norm(e, type="I"), R-Studio returns following error:

Error in norm(e, type = "1") : 'A' must be a numeric matrix

How to resolve this?

like image 978
Haroon S. Avatar asked Sep 21 '25 05:09

Haroon S.


1 Answers

To solve the problem, usee <- as.matrix(c(0.1, -0.1, 0.1)).

right below is the body of the norm function, if type!="2", it will skip to .Internal(La_dlange(x,type)), I guess this cause type 2 special but I can't give any further explain.

function (x, type = c("O", "I", "F", "M", "2")) 
{
  if (identical("2", type)) {
    svd(x, nu = 0L, nv = 0L)$d[1L]
  }
  else .Internal(La_dlange(x, type))
}
like image 101
floatsd Avatar answered Sep 22 '25 19:09

floatsd