[R] Newbie question about vector matrix multiplication

apjaworski at mmm.com apjaworski at mmm.com
Wed May 14 22:06:50 CEST 2008


Dan,

You need to do

 diag(w) %*% as.matrix(co) %*% diag(w)

The reason is that read.table creates a data frame.  Although it looks like
a matrix it is not - actually it is a special kind of list.  The
"as.matrix" function will coerce it to a matrix.

By the way, this will generate m[i,j]=w[i]*co[i,j]*w[j] but

0.000423773 7.47216E-08 4.41255E-08
7.47216E-08 1.96566E-11 4.29229E-11
4.41255E-08 4.29229E-11 4.11045E-11

is something different.

Hope this helps,

Andy

__________________________________
Andy Jaworski
518-1-01
Process Laboratory
3M Corporate Research Laboratory
-----
E-mail: apjaworski at mmm.com
Tel:  (651) 733-6092
Fax:  (651) 736-3122


                                                                           
             "Dan Stanger"                                                 
             <DStanger at eatonva                                             
             nce.com>                                                   To 
             Sent by:                  <adasgupt at mail.jci.tju.edu>         
             r-help-bounces at r-                                          cc 
             project.org               r-help at r-project.org                
                                                                   Subject 
                                       Re: [R] Newbie question about       
             05/14/2008 02:28          vector matrix multiplication        
             PM                                                            
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           




Hello Abhijit,
When I try that, I get:
> diag(w)%*%co%*%diag(w)
Error in diag(w) %*% co : requires numeric matrix/vector arguments

Dan Stanger
Eaton Vance Management
200 State Street
Boston, MA 02109
617 598 8261

-----Original Message-----
From: Abhijit Dasgupta [mailto:adasgupt at mail.jci.tju.edu]
Sent: Wednesday, May 14, 2008 3:22 PM
To: Dan Stanger
Cc: r-help at r-project.org
Subject: Re: [R] Newbie question about vector matrix multiplication

Won't diag(w)%*%co%*%diag(w) do it?

Dan Stanger wrote:
> Hello All,
>
> I have a covariance matrix, generated by read.table, and cov:
>
> co<-cov(read.table("c:/r.x"))
>
>              X            Y            Z
>
> X 0.0012517684 0.0002765438 0.0007887114
>
> Y 0.0002765438 0.0002570286 0.0002117336
>
> Z 0.0007887114 0.0002117336 0.0009168750
>
>
>
> And a weight vector generated by
>
> w<- read.table("c:/r.weights")
>
>           X         Y         Z
>
> 1 0.5818416 0.2158531 0.2023053
>
>
>
> I want to compute the product of the matrix and vectors termwise to
> generate a 3x3 matrix, where m[i,j]=w[i]*co[i,j]*w[j].
>
> 0.000423773 7.47216E-08 4.41255E-08
>
> 7.47216E-08 1.96566E-11 4.29229E-11
>
> 4.41255E-08 4.29229E-11 4.11045E-11
>
>
>
> Is this possible without writing explicit loops?
>
> Thank you,
>
> Dan Stanger
>
> Eaton Vance Management
> 200 State Street
> Boston, MA 02109
> 617 598 8261
>
>
>
>
>            [[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.
>

______________________________________________
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