this works too: n = 6 # number of rows m = 4 # number of coloumns nm = n*m mat = matrix(1:nm,n) # your matrix pf = function(Col){ ind = rep(1:(n/2),each=2) out = tapply(Col,ind,prod) out } # pf performs forall vecotr x: x[i]*x[i-1], i=2,4,6,...,n
apply(mat,2,pf) # apply pf to each coloumn of mat 2008/7/28 jim holtman <[EMAIL PROTECTED]>: > Does this do what you want: > >> x <- matrix(1:36,6) >> x > [,1] [,2] [,3] [,4] [,5] [,6] > [1,] 1 7 13 19 25 31 > [2,] 2 8 14 20 26 32 > [3,] 3 9 15 21 27 33 > [4,] 4 10 16 22 28 34 > [5,] 5 11 17 23 29 35 > [6,] 6 12 18 24 30 36 >> # create indices (going to assume an even number of rows >> x.ind <- seq(1, nrow(x), by=2) >> t(sapply(x.ind, function(.ind) x[.ind,] * x[.ind+1,])) > [,1] [,2] [,3] [,4] [,5] [,6] > [1,] 2 56 182 380 650 992 > [2,] 12 90 240 462 756 1122 > [3,] 30 132 306 552 870 1260 >> > > > On Sun, Jul 27, 2008 at 6:20 PM, rcoder <[EMAIL PROTECTED]> wrote: >> >> Hi everyone, >> >> I want to perform an operation on a matrx that outputs the product of >> successive pairs of rows. For example: calculating the product between rows >> 1 & 2; 3 & 4; 5 & 6...etc. >> >> Does anyone know of any readily available functions that can do this? >> >> Thanks, >> >> rcoder >> >> >> -- >> View this message in context: >> http://www.nabble.com/product-of-successive-rows-tp18681259p18681259.html >> Sent from the R help mailing list archive at Nabble.com. >> >> ______________________________________________ >> 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. >> > > > > -- > Jim Holtman > Cincinnati, OH > +1 513 646 9390 > > What is the problem you are trying to solve? > > ______________________________________________ > 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. > ______________________________________________ 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.