How would I multiply :
BNP.PA.Adjusted ACA.PA.Adjusted UG.PA.Adjusted
BNP.PA.Adjusted 0.010129967 0.009577789 0.007148473
ACA.PA.Adjusted 0.009577789 0.012127668 0.007340544
UG.PA.Adjusted 0.007148473 0.007340544 0.015503678
by :
c(0.3 , 0.2 , 0.5)
In order to get :
BNP.PA.Adjusted ACA.PA.Adjusted UG.PA.Adjusted
BNP.PA.Adjusted 0.010129967x0.3x0.3 0.009577789x0.2x0.3 0.007148473x0.5x0.3
ACA.PA.Adjusted 0.009577789x0.3x0.2 0.012127668x0.2x0.2 0.007340544x0.5x0.2
UG.PA.Adjusted 0.007148473x0.3x0.5 0.007340544x0.2x0.5 0.015503678x0.5x0.5
I tried using %*%
: MaMatrix <- cov_m %*% Poids
but this only does
BNP.PA.Adjusted ACA.PA.Adjusted UG.PA.Adjusted
BNP.PA.Adjusted 0.010129967x0.3 0.009577789x0.2 0.007148473x0.5
ACA.PA.Adjusted 0.009577789x0.3 0.012127668x0.2 0.007340544x0.5
UG.PA.Adjusted 0.007148473x0.3 0.007340544x0.2 0.015503678x0.5
What am I Missing ?
R has two multiplication operators for matrices. The first is denoted by * which is the same as a simple multiplication sign. This operation does a simple element by element multiplication up to matrices. The second operator is denoted by %*% and it performs a matrix multiplication between the two matrices.
To multiply a rows or columns of a matrix, we need to use %*% symbol that perform the multiplication for matrices in R. If we have a matrix M with 5 rows and 5 columns then row 1 of M can be multiplied with column 1 of M using M[1,]%*%M[,1], similarly, we can multiply other rows and columns.
To multiply only one column with a number, we can simply use the multiplication operator * but need to replace the original column with the new values.
To perform multiplication of two matrices, we should make sure that the number of columns in the 1st matrix is equal to the rows in the 2nd matrix. Therefore, the resulting matrix product will have a number of rows of the 1st matrix and a number of columns of the 2nd matrix.
An option is outer
on the vector
and then directly multiply
m1 <- outer(v1, v1)
Or as @user20650 suggested (tcrossprod
)
m1 <- tcrossprod(v1)
cov_m * m1
-output
# BNP.PA.Adjusted ACA.PA.Adjusted UG.PA.Adjusted
#BNP.PA.Adjusted 0.0009116970 0.0005746673 0.0010722709
#ACA.PA.Adjusted 0.0005746673 0.0004851067 0.0007340544
#UG.PA.Adjusted 0.0010722709 0.0007340544 0.0038759195
v1 <- c(0.3, 0.2, 0.5)
cov_m <- structure(c(0.010129967, 0.009577789, 0.007148473, 0.009577789,
0.012127668, 0.007340544, 0.007148473, 0.007340544, 0.015503678
), .Dim = c(3L, 3L), .Dimnames = list(c("BNP.PA.Adjusted", "ACA.PA.Adjusted",
"UG.PA.Adjusted"), c("BNP.PA.Adjusted", "ACA.PA.Adjusted", "UG.PA.Adjusted"
)))
You can try the code below
> diag(x) %*% cov_m %*% diag(x)
[,1] [,2] [,3]
[1,] 0.0009116970 0.0005746673 0.0010722709
[2,] 0.0005746673 0.0004851067 0.0007340544
[3,] 0.0010722709 0.0007340544 0.0038759195
Data
> dput(x)
c(0.3, 0.2, 0.5)
> dput(cov_m)
structure(c(0.010129967, 0.009577789, 0.007148473, 0.009577789,
0.012127668, 0.007340544, 0.007148473, 0.007340544, 0.015503678
), .Dim = c(3L, 3L), .Dimnames = list(c("BNP.PA.Adjusted", "ACA.PA.Adjusted",
"UG.PA.Adjusted"), c("BNP.PA.Adjusted", "ACA.PA.Adjusted", "UG.PA.Adjusted"
)))
You can also use sweep()
:
sweep(mat, 2, FUN = `*`, vec) * vec
BNP.PA.Adjusted ACA.PA.Adjusted UG.PA.Adjusted
BNP.PA.Adjusted 0.0009116970 0.0005746673 0.0010722709
ACA.PA.Adjusted 0.0005746673 0.0004851067 0.0007340544
UG.PA.Adjusted 0.0010722709 0.0007340544 0.0038759195
You can simply multiply the transpose of the matrix by the vector and transpose back again: e.g.:
m <- matrix(1:9, ncol = 3)
m
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
x <- c(2, 4, 6)
m <- t(t(m) * x) # all you need
m
[,1] [,2] [,3]
[1,] 2 16 42
[2,] 4 20 48
[3,] 6 24 54
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With