[R] Product of certain rows in a matrix without loop

PIKAL Petr petr.pikal at precheza.cz
Tue Sep 3 11:57:55 CEST 2013


Hi

one option is use embed

fff<-function(vec, n=3) apply(embed(vec,n),1,prod)
apply(A,2, fff)

Regards
Petr



> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Edouard Hardy
> Sent: Tuesday, September 03, 2013 9:34 AM
> To: R help
> Subject: [R] Product of certain rows in a matrix without loop
> 
> Hello everybody.
> Thank you again to Bert and Arun for their help on my previous
> question.
> I know have the following problem:
> I have a matrix :
> A =
> 1  2  3
> 4  5  6
> 7  8  9
> 9  8  7
> 4  5  6
> 3  2  1
> 
> And I would like to have :
> B =
> 1*4*7  2*5*8  3*6*9
> 4*7*9  5*8*8  6*9*7
> 7*9*4  8*8*5  9*7*6
> 9*4*3  8*5*2  7*6*1
> 
> Here I took the product of 3 rows each time. And 3 needs to be a
> parameter.
> 
> Is it possible to do so without any loop ?
> 
> Thank you in advance !
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.



More information about the R-help mailing list