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.
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 <mpdotbook at gmail.com> 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 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
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 <jholtman at gmail.com>:> 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 <mpdotbook at gmail.com> 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 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. >> > > > > -- > Jim Holtman > Cincinnati, OH > +1 513 646 9390 > > What is the problem you are trying to solve? > > ______________________________________________ > 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. >
Assuming that the number of rows is even and that your matrix is A, element-wise product of pairs of rows can be calculated as A[seq(1,nrow(A),by=2),]*A{seq(2,nrow(A),by=2),] --- On Mon, 28/7/08, rcoder <mpdotbook at gmail.com> wrote:> From: rcoder <mpdotbook at gmail.com> > Subject: [R] product of successive rows > To: r-help at r-project.org > Received: Monday, 28 July, 2008, 8:20 AM > 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 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.
Hi rcoder, Assuming that the number of rows of your matrix x is even, try also: x <- matrix(1:72,12) apply(x,2, tapply, rep(1:(nrow(x)/2),each=2),prod) # or using a function which argument "x" is your matrix prod.mat=function(x) { k=nrow(x) g=rep(1:(k/2),each=2) apply(x,2, tapply, g,prod) } prod.mat(x) HTH, Jorge On Sun, Jul 27, 2008 at 6:20 PM, rcoder <mpdotbook@gmail.com> 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. >[[alternative HTML version deleted]]