Sal A. Sal A. - 4 years ago 98
R Question

L1 and L2 norms in 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")
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?

Answer Source

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))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download