Hi Can someone show me an easy way to multiple a weighted vector with an matrix? example below mat1<-matrix(sample(1:100,80,replace=TRUE),ncol=8) w <- 1/1:10 I want the first element in w to be multiplied by the first row of mat1 and 2nd element in w to be multiplied with the 2nd row and so on. I have huge matrix is there an easy way other than diag(w)%*%mat1 Thanks -- View this message in context: r.789695.n4.nabble.com/Multiple-Vector-with-matrix-in-R-tp4649764.html Sent from the R help mailing list archive at Nabble.com.
Hi A.K Here is the error I get when I use %*%> dim(X)[1] 71142 219> length(Weights)[1] 71142> Wx<-diag(Weights)%*%XError in array(0, c(n, p)) : 'dim' specifies too large an array That is why I asked for different and faster method -- View this message in context: r.789695.n4.nabble.com/Multiple-Vector-with-matrix-in-R-tp4649764p4649774.html Sent from the R help mailing list archive at Nabble.com.
Hi No i didn't get error it executed too fast, I had question about the Sum squares in the weighted least square if u can help me I would I appreciated Thanks Date: Fri, 16 Nov 2012 09:46:59 -0800 From: ml-node+s789695n4649775h76@n4.nabble.com To: frespider@hotmail.com Subject: Re: Multiple Vector with matrix in R HI, Did you got the same error with sweep()? A.K. If you reply to this email, your message will be added to the discussion below: r.789695.n4.nabble.com/Multiple-Vector-with-matrix-in-R-tp4649764p4649775.html To unsubscribe from Multiple Vector with matrix in R, click here. NAML -- View this message in context: r.789695.n4.nabble.com/Multiple-Vector-with-matrix-in-R-tp4649764p4649776.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]]
Hello, Try the following. t(sapply(seq_along(w), function(i) mat1[i,]*w[i])) Hope this helps, Rui Barradas Em 16-11-2012 16:34, frespider escreveu:> Hi > > Can someone show me an easy way to multiple a weighted vector with an > matrix? > > example below > mat1<-matrix(sample(1:100,80,replace=TRUE),ncol=8) > w <- 1/1:10 > > I want the first element in w to be multiplied by the first row of mat1 and > 2nd element in w to be multiplied with the 2nd row and so on. > > I have huge matrix is there an easy way other than diag(w)%*%mat1 > > Thanks > > > > -- > View this message in context: r.789695.n4.nabble.com/Multiple-Vector-with-matrix-in-R-tp4649764.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
On Nov 16, 2012, at 8:34 AM, frespider wrote:> Hi > > Can someone show me an easy way to multiple a weighted vector with an > matrix? > > example below > mat1<-matrix(sample(1:100,80,replace=TRUE),ncol=8) > w <- 1/1:10 > > I want the first element in w to be multiplied by the first row of mat1 and > 2nd element in w to be multiplied with the 2nd row and so on. > > I have huge matrix is there an easy way other than diag(w)%*%mat1Because of argument recycling, this would be the most simple way: mat1 * w> mat1 * w == diag(w) %*% mat1[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE [2,] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE [3,] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE [4,] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE [5,] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE [6,] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE [7,] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE [8,] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE [9,] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE [10,] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE -- David Winsemius, MD Alameda, CA, USA