Hi, Suppose I have a vector in real number (x1, x2, x3, x4, x5, x6) My question is how I can get x5*x3*x1 + x6*x4*x2 ? Thanks a lot. Dot. -- View this message in context: http://www.nabble.com/how-can-I-do-this-sum--tp18931693p18931693.html Sent from the R help mailing list archive at Nabble.com.
Matlab has .*, so that t(x1,x2).*t(x3,x4).* t(x5,x6) but what is .* in R? Thanks. Dot. -- View this message in context: http://www.nabble.com/how-can-I-do-this-sum--tp18931693p18932274.html Sent from the R help mailing list archive at Nabble.com.
if you mean matrix multiplication, it's %*% but i'm not sure if that's what you meant by below ? On Mon, Aug 11, 2008 at 3:44 PM, dott wrote:> Matlab has .*, so that > t(x1,x2).*t(x3,x4).* t(x5,x6) > > but what is .* in R? > Thanks. > Dot. > > -- > View this message in context: > http://www.nabble.com/how-can-I-do-this-sum--tp18931693p18932274.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at 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.
On 12/08/2008, at 7:09 AM, dott wrote:> > Hi, > > Suppose I have a vector in real number > (x1, x2, x3, x4, x5, x6) > > My question is how I can get > x5*x3*x1 + x6*x4*x2 ?v <- c(x1, x2, x3, x4, x5, x6) m <- matrix(v,ncol=3) sum(apply(m,1,prod)) This could easily be ``generalized'' and bundled up in a function if so desired. cheers, Rolf Turner ###################################################################### Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
dott wrote:> Hi, > > Suppose I have a vector in real number > (x1, x2, x3, x4, x5, x6) > > My question is how I can get > x5*x3*x1 + x6*x4*x2 ? > > Thanks a lot. > Dot.hard to say what you mean? Maybe summing up all elements in odd positions and the ones in even positions? myvector <- 1:6 1*3*5 + 2*4*6 sum(prod(myvector[((1:length(myvector))%%2)==1]),prod(myvector[((1:length(myvector))%%2)==0])) Best, Roland