Ralph79
2008-Jan-20 23:22 UTC
[R] Efficient way for multiplying vectors with a only certain number of rows in a matrix
Dear R-users, I am working on a problem that I am currently not able to solve efficiently. It is about multiplying one column of a matrix with only a certain number of rows of another matrix. Let me illustrate my problem with an example: n.obs = 800 n.rowsperobs = 300 n.param = 23 Designmat = matrix(rnorm(n.obs*n.rowsperobs*n.param),ncol=n.param) Betamat = matrix(rnorm(n.obs*n.param),nrow=n.param) In this example, "Designmat" consists of 800*300 rows, meaning that 300 rows belong to one of the 800 observations. Each observation has also one parameter vector, which is one column in the "Betamat"-matrix (i.e. Betamat contains the n.obs parameter vectors). My goal is to multiply the parameter vector of each observation (i.e. the respective column in Betamat) with ONLY THOSE ROWS IN DESIGNMAT that belong to this observation. Applied to the example above: The first column in Betamat has to be multiplied with the first 300 rows in Designmat, the second column in Betamat has to be multiplied with rows 301 to 600 in Betamat and so on. Hence, the result of this operation should be a vector of length 240000. I can think of solutions implying several loops and/or lapplys, but I guess that there might be a much easyer and above all faster solution. Thank you very much for your help in advance. -- View this message in context: http://www.nabble.com/Efficient-way-for-multiplying-vectors-with-a-only-certain-number-of-rows-in-a-matrix-tp14988427p14988427.html Sent from the R help mailing list archive at Nabble.com.
Henrique Dallazuanna
2008-Jan-21 01:39 UTC
[R] Efficient way for multiplying vectors with a only certain number of rows in a matrix
Perhaps: n.obs = 800 n.rowsperobs = 300 n.param = 23 Designmat = matrix(rnorm(n.obs*n.rowsperobs*n.param),ncol=n.param) Betamat = matrix(rnorm(n.obs*n.param),nrow=n.param) Design.spl <- split(as.data.frame(Designmat), rep(1:n.obs, each=n.rowsperobs)) res <- sapply(1:ncol(Betamat), function(x)as.matrix(Design.spl[[x]])%*%Betamat[,x]) On 20/01/2008, Ralph79 <ralph.statistics at gmx.net> wrote:> > Dear R-users, > > I am working on a problem that I am currently not able to solve efficiently. > It is about multiplying one column of a matrix with only a certain number of > rows of another matrix. > > Let me illustrate my problem with an example: > > n.obs = 800 > n.rowsperobs = 300 > n.param = 23 > > Designmat = matrix(rnorm(n.obs*n.rowsperobs*n.param),ncol=n.param) > > Betamat = matrix(rnorm(n.obs*n.param),nrow=n.param) > > In this example, "Designmat" consists of 800*300 rows, meaning that 300 rows > belong to one of the 800 observations. > > Each observation has also one parameter vector, which is one column in the > "Betamat"-matrix (i.e. Betamat contains the n.obs parameter vectors). > > My goal is to multiply the parameter vector of each observation (i.e. the > respective column in Betamat) with ONLY THOSE ROWS IN DESIGNMAT that belong > to this observation. > Applied to the example above: The first column in Betamat has to be > multiplied with the first 300 rows in Designmat, the second column in > Betamat has to be multiplied with rows 301 to 600 in Betamat and so on. > Hence, the result of this operation should be a vector of length 240000. > > I can think of solutions implying several loops and/or lapplys, but I guess > that there might be a much easyer and above all faster solution. > > Thank you very much for your help in advance. > > -- > View this message in context: > http://www.nabble.com/Efficient-way-for-multiplying-vectors-with-a-only-certain-number-of-rows-in-a-matrix-tp14988427p14988427.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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Ralph79
2008-Jan-23 11:42 UTC
[R] Efficient way for multiplying vectors with a only certain number of rows in a matrix
Just in case somebody is interested: I found out an efficient way for doing the calculations mentioned below. The code is: n.obs = 800 n.rowsperobs = 300 n.param = 23 Designmat = matrix(rnorm(n.obs*n.rowsperobs*n.param),ncol=n.param) Betamat = t(matrix(rnorm(n.obs*n.param),nrow=n.param)) res=rowSums(Designmat*Betamat2[rep(1:n.obs,rep(n.rowsperobs,n.obs)),]) If somebody can think of an even faster way: any comments are greatly welcome! Ralph79 wrote:> > Dear R-users, > > I am working on a problem that I am currently not able to solve > efficiently. It is about multiplying one column of a matrix with only a > certain number of rows of another matrix. > > Let me illustrate my problem with an example: > > n.obs = 800 > n.rowsperobs = 300 > n.param = 23 > > Designmat = matrix(rnorm(n.obs*n.rowsperobs*n.param),ncol=n.param) > > Betamat = matrix(rnorm(n.obs*n.param),nrow=n.param) > > In this example, "Designmat" consists of 800*300 rows, meaning that 300 > rows belong to one of the 800 observations. > > Each observation has also one parameter vector, which is one column in the > "Betamat"-matrix (i.e. Betamat contains the n.obs parameter vectors). > > My goal is to multiply the parameter vector of each observation (i.e. the > respective column in Betamat) with ONLY THOSE ROWS IN DESIGNMAT that > belong to this observation. > Applied to the example above: The first column in Betamat has to be > multiplied with the first 300 rows in Designmat, the second column in > Betamat has to be multiplied with rows 301 to 600 in Betamat and so on. > Hence, the result of this operation should be a vector of length 240000. > > I can think of solutions implying several loops and/or lapplys, but I > guess that there might be a much easyer and above all faster solution. > > Thank you very much for your help in advance. > >-- View this message in context: http://www.nabble.com/Efficient-way-for-multiplying-vectors-with-a-only-certain-number-of-rows-in-a-matrix-tp14988427p15039994.html Sent from the R help mailing list archive at Nabble.com.