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

Christoph Lehmann christoph.lehmann at gmx.ch
Tue Feb 15 15:45:29 CET 2005


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




More information about the R-help mailing list