Good question! As I'm sure you've seen, you can't just do big_array * small_mat
because the dimensions don't match, and R requires matching dimensions when doing array/matrix arithmetic.
One solution is to reshape small_mat
to have that third dimension of "depth" 3
.
library(mvtnorm)
big_array <- array(
sapply(
rep(2, 5), rmvnorm, sigma = diag(2)
), dim = c(5, 2, 3)
)
small_mat <- rmvnorm(5, sigma = diag(2))
# sigh
big_array * small_mat
#> Error in big_array * small_mat: non-conformable arrays
small_mat_reshaped <- array(small_mat, c(5, 2, 3))
small_mat_reshaped
#> , , 1
#>
#> [,1] [,2]
#> [1,] 1.6497271 1.1020500
#> [2,] 0.8772834 -0.8751448
#> [3,] 0.9883818 0.5546976
#> [4,] 0.5238883 -1.1669689
#> [5,] -0.5959466 0.7387694
#>
#> , , 2
#>
#> [,1] [,2]
#> [1,] 1.6497271 1.1020500
#> [2,] 0.8772834 -0.8751448
#> [3,] 0.9883818 0.5546976
#> [4,] 0.5238883 -1.1669689
#> [5,] -0.5959466 0.7387694
#>
#> , , 3
#>
#> [,1] [,2]
#> [1,] 1.6497271 1.1020500
#> [2,] 0.8772834 -0.8751448
#> [3,] 0.9883818 0.5546976
#> [4,] 0.5238883 -1.1669689
#> [5,] -0.5959466 0.7387694
big_array * array(small_mat, c(5, 2, 3))
#> , , 1
#>
#> [,1] [,2]
#> [1,] -0.27196864 0.4807641
#> [2,] -0.08161026 -1.4298487
#> [3,] -0.73607024 -0.0011445
#> [4,] -0.19076169 0.2165741
#> [5,] 0.34588880 0.9547587
#>
#> , , 2
#>
#> [,1] [,2]
#> [1,] 0.06962186 -1.6031662
#> [2,] 0.73160661 0.2134017
#> [3,] 0.31997080 0.5269535
#> [4,] 0.50023436 -1.2813516
#> [5,] 1.11332967 -1.4334219
#>
#> , , 3
#>
#> [,1] [,2]
#> [1,] -0.27196864 0.4807641
#> [2,] -0.08161026 -1.4298487
#> [3,] -0.73607024 -0.0011445
#> [4,] -0.19076169 0.2165741
#> [5,] 0.34588880 0.9547587