 # Multiplying multiple R objects

I have created three objects in r (each a 3x4 matrix). I need to do some simple arithmetic on the objects. I want to create a new object that has the same 3x4 structure by multiplying each object by each respective element in each object. For example, WCS_New 1,X1 = [WCS 1,X1 times WSCH 1,X1 times WCSC 1,X1] , WCS_New 1,X2 = [WCS 1,X2 times WSCH 1,X2 times WCSC 1,X2] ... WCS_New 4,X3 = [WCS 4, X3 times WSCH 4, X3 times WCSC 4,X3]. My understanding is that this is not matrix multiplication. It's analogous to multiplying the same cell across different worksheets in Excel.

I am struggling with how to code this in R, and was hoping there is an easy solution. The actual objects are much bigger (5000 x 1000), so I wanted to avoid loops. I'm rather new at this so I apologize for the convoluted explanation. A picture of the objects are as follows:

WCS WCSH WCSC And the reprex code is as follows:

``````library(tidyverse)
library(reshape2)
library(tidybayes)
library(Sim.DiffProc)
library(VineCopula)
library(ESGtoolkit)
library(reprex)

# Create monte carlo simulation for WCS prices
set.seed(4580)
WCS <- ABM(N =3,M=3,x0=-21.50,t0=0,T=96,Dt=NULL,theta=-0.0001,sigma=0.30)

# Create monte carlo simulation for WCS H
set.seed(4580)
WCSH <- ABM(N =3,M=3,x0=-6.75,t0=0,T=96,Dt=NULL,theta=-0.0001,sigma=0.10)

# Create monte carlo simulation for WCS Har
set.seed(4580)
WCSC <- ABM(N =3,M=3,x0=-8.00,t0=0,T=96,Dt=NULL,theta=-0.0001,sigma=0.10)
``````

Actually it is, but it is a Hadamard product, take a look to this function

``````library(Sim.DiffProc)
library(matrixcalc)
library(magrittr)

set.seed(4580)
WCS <- ABM(N =3,M=3,x0=-21.50,t0=0,T=96,Dt=NULL,theta=-0.0001,sigma=0.30)

# Create monte carlo simulation for WCS H
set.seed(4580)
WCSH <- ABM(N =3,M=3,x0=-6.75,t0=0,T=96,Dt=NULL,theta=-0.0001,sigma=0.10)

# Create monte carlo simulation for WCS Har
set.seed(4580)
WCSC <- ABM(N =3,M=3,x0=-8.00,t0=0,T=96,Dt=NULL,theta=-0.0001,sigma=0.10)

WCS %>%
#> Time Series:
#> Start = 0
#> End = 96
#> Frequency = 0.03125
#>    Xmat.Xmat.X1 Xmat.Xmat.X2 Xmat.Xmat.X3
#>  0   -1161.0000   -1161.0000   -1161.0000
#> 32    -966.9450   -1186.5298   -1234.3243
#> 64    -898.6709    -782.8184   -1009.0090
#> 96    -794.6527   -1064.5337    -898.9361
``````

Created on 2019-07-19 by the reprex package (v0.3.0)

1 Like

I think regular multiplication does what you want. It's vectorized, so it multiplies pairs of elements along vectors.

Example:

``````set.seed(21)
m1 <- matrix(rpois(12, 5), nrow = 3)
m2 <- matrix(rpois(12, 5), nrow = 3)
m3 <- matrix(rpois(12, 5), nrow = 3)
m1
#      [,1] [,2] [,3] [,4]
# [1,]    7    3    2    7
# [2,]    3    9    3    6
# [3,]    6    8   10    9
m2
#      [,1] [,2] [,3] [,4]
# [1,]    2    1    5    5
# [2,]    6    5    6    7
# [3,]    3    4    1    8
m3
#      [,1] [,2] [,3] [,4]
# [1,]    4    7    9    7
# [2,]    7    6    5    8
# [3,]    5    6    6    3
m_combined <- m1 * m2 * m3
m_combined
#      [,1] [,2] [,3] [,4]
# [1,]   56   21   90  245
# [2,]  126  270   90  336
# [3,]   90  192   60  216
``````

So `m_combined[1, 1]` is equal to `m1[1, 1] * m2[1, 1] * m3[1, 1]`, and `m_combined[2, 1]` is equal to `m1[2, 1] * m2[2, 1] * m3[2, 1]`, and so on.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.