Sophie Baillargeon
2006-Sep-08 13:21 UTC
[R] Multiple matrix multiplication with two 3-dimensional arrays
Hi, I need to do several matrix multiplications with the corresponding matrices forming two 3-dimentional arrays. To illustrate my problem, let's say I have the following 3-dimensional arrays: array1 <- array(1:30,dim=c(3,2,5)) array2 <- array(1:20,dim=c(2,2,5)) I know that I can get what I want with the following computation : result <- array(dim=c(dim(array1)[1], dim(array2)[2], dim(array1)[3])) for (i in 1: dim(array1)[3]) { result[,,i] <- array1[,,i]%*%array2[,,i] } My question is : Is there a more efficient way to do that computation, i.e. without a loop? Maybe I could use an "apply" or something but I can't figure out how. I would have hoped that simply doing array1%*%array2 would work, but it doesn?t Thank you very much! Sophie Baillargeon __________________________________________ Sophie Baillargeon, M.Sc. Professionnelle de recherche en statistique D?partement de math?matiques et de statistique Universit? Laval t?l?phone: (418) 656-2131 poste 2333 courriel: Sophie.Baillargeon at mat.ulaval.ca
Gabor Grothendieck
2006-Sep-08 13:59 UTC
[R] Multiple matrix multiplication with two 3-dimensional arrays
If the arrays are a1 and a2 then: library(abind) abind(lapply(1:dim(a1)[3], function(i) a1[,,i] %*% a2[,,i]), along = 3) On 9/8/06, Sophie Baillargeon <Sophie.Baillargeon at mat.ulaval.ca> wrote:> > Hi, > > I need to do several matrix multiplications with > the corresponding matrices forming two > 3-dimentional arrays. To illustrate my problem, > let's say I have the following 3-dimensional arrays: > > array1 <- array(1:30,dim=c(3,2,5)) > array2 <- array(1:20,dim=c(2,2,5)) > > I know that I can get what I want with the following computation : > > result <- array(dim=c(dim(array1)[1], dim(array2)[2], dim(array1)[3])) > for (i in 1: dim(array1)[3]) > { > result[,,i] <- array1[,,i]%*%array2[,,i] > } > > My question is : > Is there a more efficient way to do that computation, i.e. without a loop? > > Maybe I could use an "apply" or something but I > can't figure out how. I would have hoped that simply doing > > array1%*%array2 > > would work, but it doesn't? > > > Thank you very much! > > > Sophie Baillargeon > > __________________________________________ > > Sophie Baillargeon, M.Sc. > > Professionnelle de recherche en statistique > D?partement de math?matiques et de statistique > Universit? Laval > t?l?phone: (418) 656-2131 poste 2333 > courriel: Sophie.Baillargeon at mat.ulaval.ca > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Anupam Tyagi
2006-Sep-09 05:11 UTC
[R] Multiple matrix multiplication with two 3-dimensional arrays
Sophie Baillargeon <Sophie.Baillargeon <at> mat.ulaval.ca> writes:> Maybe I could use an "apply" or something but I > can't figure out how. I would have hoped that simply doing > > array1%*%array2 > > would work, but it doesn?tI think one of the issues is that algebra for N-Dimentional arrays are not well defined. Think how would you define the above operartion on two 3x3x3 arrays. I had seen a paper a couple of years ago in some math journal that had proposed an algebra. I will be interested in knowing a reference to some source that defines 3-dimentional array algebra. Then perhaps it could be implemented in R. I will also be interested in knowing what is the equivalent of "cell" array in MATLAB in R---that is something that is equivalent to the "cell" functions in a spreadsheet---sometimes this can be useful. Anupam.