Can I have some help in vectorizing a series of matrix multiplications?
Toy Example

mat_size=2; num_matrices=3; num_users=2

ToyArray=array(1,dim=c(mat_size,   mat_size, num_matrices, num_users))
/* So I open an 4-dim array to store 3  2 X 2 matrrices  for 2 users.  For each user I want to multiple the 3, 2 X 2 matrices so that  at the end I have 1 matrix for each of the 2 users */

This works:
 output=array(NA,dim=c(mat_size, mat_size, num_users));

 for ( i in 1:num_users) {
  output[,,i]= as.matrix(ToyArray[,,1,i]) %*% as.matrix(ba[,,2,i]) %*% as.matrix(ba[,,3,i])
 };

Can I do better? Can I vectorize this over the user dimension?


And even better (and a different question): what strategy can I use if the matrices are not all of the same  size?

GR
	[[alternative HTML version deleted]]

