简体   繁体   中英

Elements-wise matrix algebra in R arrays

I have two arrays with dimensions: arry1[2,2,n] and array2[2,2,n] . That is, n two by two matrices.

I want to produce a third array which is the n-element-wise matrix multiplication (ie, %*%) of array1 and array2 . Producing yet another array with dimensions: array3[2,2,n] .

Frustratingly, I cannot figure out how to use %*% to pull this off, the following doesn't seem to work

array3 <- array1[1:2,1:2,]%*%array2[1:2,1:2,]

Moreover, the apply() family of functions don't appear to enable my operation. Any help would be greatly appreciated.

If I understood correctly, this will work

array(sapply(1:5,function(x) a1[,,x]%*%a2[,,x]),dim = c(2,2,5))

Data

a1=array(outer(outer(1:2,1:2),1:5),dim = c(2,2,5))
a2=array(outer(outer(1:2,1:2),1:5),dim = c(2,2,5))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM