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

From: Dimitris Rizopoulos <Dimitris.Rizopoulos_at_med.kuleuven.be>
Date: Wed, 14 May 2008 21:31:03 +0200

try this:

V <- var(matrix(rnorm(100*3), 100, 3))
w <- c(0.5, 0.3, 0.2)

V * (w %o% w)

I hope it helps.

Best,
Dimitris

Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

```Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
http://www.student.kuleuven.be/~m0390867/dimitris.htm

```

Quoting Dan Stanger <DStanger_at_eatonvance.com>:

> 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
>
>
>
> 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