How can I get one 3d array from two 2d arrays?
So, given A
and B
:
(A <- matrix(1:6, nrow = 3, ncol = 2, dimnames = list(c("A", "B", "C"), c("D", "E"))))
D E
A 1 4
B 2 5
C 3 6
(B <- matrix(1:8, nrow = 4, ncol = 2, dimnames = list(c("F", "G", "H", "I"), c("D", "E"))))
D E
F 1 5
G 2 6
H 3 7
I 4 8
I want to get a new 3d array containing the outer products of the column vectors. Here is a hardcoded example of expected result:
(outer(A[,"D"], B[,"D"]))
F G H I
A 1 2 3 4
B 2 4 6 8
C 3 6 9 12
(outer(A[,"E"], B[,"E"]))
F G H I
A 20 24 28 32
B 25 30 35 40
C 30 36 42 48
I can do a code-it-up, but there must be a functional approach to solve this?