On Tue, 2007-11-20 at 17:13 -0800, Anup Nandialath wrote:
> Dear Friends,
> 
> My objective is to do element wise multiplication of two vectors. For example 
> suppose I have 
> 
> a <- (1,1,1)
> b <- (2,4)
> 
> My output should be (2,4,2,4,2,4). I managed to write it down with loops as 
> follows
> 
> r <- c(1,1,1)
> l <- c(2,4)
> x <- 1
> for (j in 1:3)
> {
>   for (i in 1:2)
>     {
>       new[x,] <- r[j]*l[i]
>       x <- x+1
>     }
> }
> 
> 
> Is there a simpler solution to this without using the loops?
> 
> 
> Thanks and Regards
> 
> Anup

Try this:

> as.vector(t(a %o% b))
[1] 2 4 2 4 2 4


See ?outer for more information.

HTH,

Marc Schwartz

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to