I have 3 matrices:
A (n by K),
B (L by m) and
C (L by K)
and would like to produce a 4th matrix
D (n by m)
with elements
D(i,j) = sum(B[,i,drop=FALSE]%*%A[j,,drop=FALSE] * C)
(Notice that B[,i,drop=FALSE]%*%A[j,,drop=FALSE] is the product of a (L by 1) matrix with a (1 by K_ matrix, and hence is (L by K), as C is. “sum” sums all elements of the resulting matrix)
One way of doing this is creating a grid as expand.grid(1:n,1:m) and calculating D(.,.) for each of these elements. Any ideas of how to do it faster in R?
Thanks!
1 Answer