[R] Multiplying each row of a big matrix with a vector

Attiglah, Mama Mama_Attiglah at ssga.com
Wed Jan 30 12:32:05 CET 2008


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.



More information about the R-help mailing list