Suppose I have two matrices x and y, both with dimensions 100x2. I would like to create a list such that for each row of x and y, I have the matrix t(x) %*% y. For example, via a for loop:
x = matrix(rnorm(10), nrow = 5)
y = matrix(rnorm(10), nrow = 5)
myList = list()
for(i in 1:5){
myList[[i]] = t(x[i, , drop = FALSE]) %*% y[i, ]
}
Is there a more efficient way to do this calculation? I've tried to figure out how to express this a matrix multiplication but have had no luck. I've also considered mapply, but it seems as if I'd need to convert x and y to lists of vectors instead of matrices to use mapply, and I'm skeptical that that is the correct approach either.
Map(function(x,y) matrix(x,ncol=1)%*%y , split(x, row(x)), split(y, row(y)))- akrunmylistobject which will make your for loop approach noticeably faster. Usemylist = vector("list", 5)- talat