[R] A slightly unorthodox matrix product.

Berry, Charles ccberry @end|ng |rom uc@d@edu
Sat Aug 4 19:34:26 CEST 2018



> On Aug 4, 2018, at 10:01 AM, Eric Berger <ericjberger using gmail.com> wrote:
> 
> Hi Rolf,
> A few edits because (i) nrow(a) should be nrow(A) and (ii) you have
> calculated C[j,k,i] = A[i,j]*B[i,k], (iii) minor style change on lapply.
> 
> library(abind)
> xxx <- lapply(1:nrow(A),function(i){A[i,]%o%B[i,]})
> yyy <- do.call(abind,c(xxx,list(along=3)))

Or use the simplify="array" gambit in sapply:

yyy <- sapply(1:nrow(A), function(i) A[i,] %o% B[i,], simplify="array")

> zzz <- aperm(yyy,c(3,1,2))
> 

HTH, 

Chuck




More information about the R-help mailing list