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

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.ac.be
Tue Feb 15 16:04:19 CET 2005


you are trying to square an expression not an number! try this:

h.fun <- function(tt) ((tt/d1)^a1) * exp(-(tt-d1)/b1) - 
cc*((tt/d2)^a2) * exp(-(tt-d2)/b2)

h <- h.fun(tt)
h <- h/sqrt(sum(h*h))

I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/16/336899
Fax: +32/16/337015
Web: http://www.med.kuleuven.ac.be/biostat/
     http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm



----- Original Message ----- 
From: "Christoph Lehmann" <christoph.lehmann at gmx.ch>
To: <R-help at stat.math.ethz.ch>
Sent: Tuesday, February 15, 2005 3:45 PM
Subject: [R] matlab norm(h) command in R: sqrt(sum(h^2)) - use in 
anexpression


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