# Re: [R] Newbie question about vector matrix multiplication

From: Jeremiah Rounds <jeremiahrounds_at_hotmail.com>
Date: Wed, 14 May 2008 12:53:26 -0700

> Date: Wed, 14 May 2008 15:18:32 -0400
> From: DStanger_at_eatonvance.com
> To: r-help_at_r-project.org
> Subject: [R] Newbie question about vector matrix multiplication
>
> Hello All,
>
> I have a covariance matrix, generated by read.table, and cov:
>
>
> 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
>
>
> 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

First off your example matrix does not seem to represent the equation you wrote down. For example m[1,3] should be m[1,3] = 0.5818416 * 0.0007887114* 0.2023053 = .000928. I apologize if that represents something incorrect on part. However if I am correct then I believe what you seek is the line below:

m = w %*% t(w)*co

To get there btw picture moving the weights together then picture multiplying two equal sized matrixes together by each coefficient.

>
>
>
> 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
> and provide commented, minimal, self-contained, reproducible code.

esh_messenger_052008

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. Received on Wed 14 May 2008 - 23:13:47 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 14 May 2008 - 23:30:38 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.