[R] A slightly unorthodox matrix product.

Eric Berger er|cjberger @end|ng |rom gm@||@com
Sat Aug 4 19:01:02 CEST 2018


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)))
zzz <- aperm(yyy,c(3,1,2))

HTH,
Eric


On Sat, Aug 4, 2018 at 8:52 AM, Rolf Turner <r.turner using auckland.ac.nz> wrote:

>
> Can anyone think of a sexy way of forming following "product"?
>
> Given matrices A and B, both with m rows, form a 3 dimensional array C
> such that:
>
>     C[i,j,k] = A[i,j]*B[i,k]
>
> I *think* that the following does what I want.  (I keep confusing myself,
> so I'm not sure!)
>
> library(abind)
> xxx <- lapply(1:nrow(a),function(i,a,b){a[i,]%o%b[i,]},a=A,b=B)
> do.call(abind,c(xxx,list(along=3)))
>
> Is there a cleverer way?
>
> cheers,
>
> Rolf Turner
>
> --
> Technical Editor ANZJS
> Department of Statistics
> University of Auckland
> Phone: +64-9-373-7599 ext. 88276
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posti
> ng-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]




More information about the R-help mailing list