[R] matlab norm(h) command in R: sqrt(sum(h^2)) - use in an e xpression

Liaw, Andy andy_liaw at merck.com
Tue Feb 15 15:56:08 CET 2005


> From: Christoph Lehmann
> 
> Hi
> 
> in matlab I defined a function (double gamma, parameters at 
> the end of 
> this mail) as
>    h(i)=((t/d1)^a1)*exp(-(t-d1)/b1)-c*((t/d2)^a2)*exp(-(t-d2)/b2);
>    h=h/norm(h);
> 
> I do know that norm() in matlab is equal to:
> 
>    sqrt(sum(x^2))
> in R
> so in R I do it like:
> 
> #function (double gamama)
> h <- expression((t/d1)^a1*exp(-(t-d1)/b1)-c*(t/d2)^a2*exp(-(t-d2)/b2))
> 
> # plot it
> t <- seq(0, 20000, by = 100)
> t <- t/1000
> plot(eval(h), type = 'l')
> 
> # however this yields an error
> h <- h/sqrt(sum(h^2))
       ^          ^

Those are still expressions, not vectors.  You need to wrap those in eval()
as you did in plot(), or do h <- eval(h) beforehand.

Andy




> Error in h^2 : non-numeric argument to binary operator
> 
> what shall I do to get the matlab: h = h/norm(h) implemented in R?
> 
> thanks for a hint
> 
> christoph
> 
> 
> ----
> # parameters
> peak1 <- 5.4
> fwhm1 <- 5.2
> peak2 <- 10.8
> fwhm2 <- 7.35
> dip <- 0.35
> 
> b1 <- 0.9 # dispersion
> b2 <- 0.9 #dispersion
> a1 <- peak1/b1
> a2 <- peak2/b2
> d1 <- a1*b1
> d2 <- a2*b2
> c <- dip
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
> 
>




More information about the R-help mailing list