I have a big matrix 'ret'. I want to multiply each row of it with a 2nd vector 'pos', resulting result, I want to save in a vector named 'port'. I wrote following code: > pos [1] 2593419 2130220 6198197 1673888 1980000 1784732 2052120 -7490228 -5275000 > dim(ret) [1] 500 9 > fu # user defined function function(x) { fu = x %*% t(pos) } port = apply(ret, 1, fu) > dim(port) [1] 81 500 My desire is to get port as a vector with length 500. However I am not getting that? Can anyone tell me how to correct that? Regards, --------------------------------- [[alternative HTML version deleted]]
Bill.Venables at csiro.au
2008-Jan-30 07:52 UTC
[R] Multiplying each row of a big matrix with a vector
port <- as.vector(ret %*% port) should do it. Bill Venables -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Megh Dal Sent: Wednesday, 30 January 2008 5:20 PM To: r-help at stat.math.ethz.ch Subject: [R] Multiplying each row of a big matrix with a vector I have a big matrix 'ret'. I want to multiply each row of it with a 2nd vector 'pos', resulting result, I want to save in a vector named 'port'. I wrote following code: > pos [1] 2593419 2130220 6198197 1673888 1980000 1784732 2052120 -7490228 -5275000 > dim(ret) [1] 500 9 > fu # user defined function function(x) { fu = x %*% t(pos) } port = apply(ret, 1, fu) > dim(port) [1] 81 500 My desire is to get port as a vector with length 500. However I am not getting that? Can anyone tell me how to correct that? Regards, --------------------------------- [[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.
Petr PIKAL
2008-Jan-30 07:53 UTC
[R] Odp: Multiplying each row of a big matrix with a vector
I am not sure if you really want what you explain that you want To multiply each row with a vector you can use e.g. t(apply(ret,1, function(y) pos*y)) or t(t(ret)*pos) but then you will get the same matrix just multiplied by a vector. If you want to perform matrix multiplication then just ret %*% pos will do it for you Petr Pikal petr.pikal at precheza.cz r-help-bounces at r-project.org napsal dne 30.01.2008 08:20:21:> I have a big matrix 'ret'. I want to multiply each row of it with a 2ndvector> 'pos', resulting result, I want to save in a vector named 'port'. Iwrote> following code: > > > pos > [1] 2593419 2130220 6198197 1673888 1980000 1784732 2052120-7490228 -5275000> > > > dim(ret) > [1] 500 9 > > > fu # user defined function > function(x) > { > fu = x %*% t(pos) > } > port = apply(ret, 1, fu) > > > dim(port) > [1] 81 500 > > My desire is to get port as a vector with length 500. However I am notgetting that?> > Can anyone tell me how to correct that? > > Regards, > > > > --------------------------------- > > [[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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Robin Hankin
2008-Jan-30 08:56 UTC
[R] "=" in functions and matlab (was: Multiplying each row of a big matrix with a vector)
hits=-2.6 tests=BAYES_00 X-USF-Spam-Flag: NO [this after a mistakenly private email to Megh] As others have said, "%*%" is sufficiently vectorized to do what you want. Speaking as a reformed Matlab user, it might be as well to add that the definition of function fu() that you give looks like matlab code to me. Check this out: > f <- function(x){f = x^2} > f(4) > > dput(f(4)) 16 > Thus fu() returns 16 but invisibly. This can be terribly confusing to those from a matlab background. In R, one can just write > f<- function(x){x^2} because functions return the last evaluated expression. Compare matlab, where(IIRC) one has to state in the function the name of the variable whose value will be returned. HTH rksh On 30 Jan 2008, at 07:20, Megh Dal wrote:> I have a big matrix 'ret'. I want to multiply each row of it with a > 2nd vector 'pos', resulting result, I want to save in a vector named > 'port'. I wrote following code: > >> pos > [1] 2593419 2130220 6198197 1673888 1980000 1784732 2052120 > -7490228 -5275000 > > >> dim(ret) > [1] 500 9 > >> fu # user defined function > function(x) > { > fu = x %*% t(pos) > } > port = apply(ret, 1, fu) > >> dim(port) > [1] 81 500 > > My desire is to get port as a vector with length 500. However I am > not getting that? > > Can anyone tell me how to correct that? > > Regards, > > > > --------------------------------- > > [[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.-- Robin Hankin Uncertainty Analyst and Neutral Theorist, National Oceanography Centre, Southampton European Way, Southampton SO14 3ZH, UK tel 023-8059-7743
Attiglah, Mama
2008-Jan-30 11:32 UTC
[R] Multiplying each row of a big matrix with a vector
Ret= matrix(sample( 1:1000, 500*9), nrow=500, ncol=9) Pos= c( 2593419 ,2130220, 6198197, 1673888, 1980000 , 1784732 , 2052120 ,-7490228 ,-5275000) Solution = Ret * matrix( rep(Pos, 500), nrow=500, byrow=TRUE) Use the element-wise multiplication rather than a matrix multiplication. Hope this helps. Mama ----- Mama Attiglah, PhD Advanced Research Center Quantitative Research Analyst State Street Bank +44(0)20 7698 6290 (Direct Line) +44 (0)207 004 2968 (Direct Fax) Please visit our Web site at www.ssga.com -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Megh Dal Sent: 30 January 2008 07:20 To: r-help at stat.math.ethz.ch Subject: [R] Multiplying each row of a big matrix with a vector I have a big matrix 'ret'. I want to multiply each row of it with a 2nd vector 'pos', resulting result, I want to save in a vector named 'port'. I wrote following code: > pos [1] 2593419 2130220 6198197 1673888 1980000 1784732 2052120 -7490228 -5275000 > dim(ret) [1] 500 9 > fu # user defined function function(x) { fu = x %*% t(pos) } port = apply(ret, 1, fu) > dim(port) [1] 81 500 My desire is to get port as a vector with length 500. However I am not getting that? Can anyone tell me how to correct that? Regards, --------------------------------- [[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.