[R] Corrected: approaches to matrix multiplication of each layer of 3-d array with different matrix

Ranjan Maitra maitra.mbox.ignored at inbox.com
Fri Jul 10 17:37:37 CEST 2015


What does it mean to unroll the loop?

Thanks!

Ranjan


On Fri, 10 Jul 2015 07:07:15 -0700 Jeff Newmiller <jdnewmil at dcn.davis.ca.us> wrote:

> Strictly speaking, the answer is yes because you can unroll the loop, but that probably is not what you really want or need to do.
> 
> Your example seems about right, but it is not clear how you plan to make 44 conform with 5500.
> ---------------------------------------------------------------------------
> Jeff Newmiller                        The     .....       .....  Go Live...
> DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
>                                       Live:   OO#.. Dead: OO#..  Playing
> Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
> /Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
> ---------------------------------------------------------------------------
> Sent from my phone. Please excuse my brevity.
> 
> On July 10, 2015 6:44:19 AM PDT, Ranjan Maitra <maitra.mbox.ignored at inbox.com> wrote:
> >Hi,
> >
> >Sorry to post again, but there is a careless error in my first R code
> >snippet:
> >
> >--- begin R code ---
> > 
> >AA <- array(1:60, dim = c(5500,44,33))
> >BB <- array(1:60, dim = c(44,44,33))
> > 
> > 
> >arraymatprod <- function(A, B) {
> >    EE <- array(dim=dim(A));
> >    for (i in 1:dim(A)[3]) EE[,,i] <- A[,,i] %*% B[,,i]
> >    EE
> >}
> >
> >system.time(arraymatprod(AA, BB))
> >
> >--- end R code ---
> >
> >The second snippet is correct:
> >
> >--- begin R code ---
> >
> >arraymatrixproduct <- function(A, B) {
> >	require(abind)
> >	dA <- dim(A)[1]
> >	AB <- abind(A, B, along = 1)
> >	array(apply(X = AB, MARGIN = 3, FUN = (function(mat) ((mat[1:dA, ] %*%
> >mat[(dA+1):(dim(AB)[1]),])))), dim = dim(A))
> >}
> >
> >system.time(arraymatrixproduct(AA, BB))
> >
> >--- end R code ---
> >
> >However, the second is almost twice as long as the first snippet.
> >
> >Many thanks,
> >Ranjan
> >
> >
> >On Fri, 10 Jul 2015 08:23:49 -0500 Ranjan Maitra
> ><maitra.mbox.ignored at inbox.com> wrote:
> >
> >> Dear friends,
> >> 
> >> I have two 3-d arrays of appropriate dimensions. I want to
> >postmultiply the 2-D matrix layers of the first with the 2-D matrix
> >layers of the second. Can I do this easily in R avoiding loops?
> >> 
> >> As an example:
> >> 
> >> --- begin R code ---
> >> 
> >> AA <- array(1:60, dim = c(5500,44,33))
> >> BB <- array(1:60, dim = c(44,44,33))
> >> 
> >> 
> >> arraymatprod <- function(A, B) {
> >>     EE <- array(dim=dim(A));
> >>     for (i in 1:3) EE[,,i] <- A[,,i] %*% B[,,i]
> >>     EE
> >> }
> >> 
> >> system.time(arraymatprod(AA, BB))
> >> 
> >> --- end R code ---
> >> 
> >> So, is there a way to do this without the loop?
> >> 
> >> Of course, I could abind the arrays and then use apply with an
> >appropriate function which would be as follows:
> >> 
> >> --- begin R code ---
> >> 
> >> arraymatrixproduct <- function(A, B) {
> >> 	require(abind)
> >> 	dA <- dim(A)[1]
> >> 	AB <- abind(A, B, along = 1)
> >> 	array(apply(X = AB, MARGIN = 3, FUN = (function(mat) ((mat[1:dA, ]
> >%*% mat[(dA+1):(dim(AB)[1]),])))), dim = dim(A))
> >> }
> >> 
> >> system.time(arraymatrixproduct(AA, BB))
> >> 
> >> --- end R code ---
> >> 
> >> However, this turns out to be slower -- perhaps because of the use of
> >abind and filling the array inside the function.
> >> 
> >> I just wanted suggestions to get this operation done more
> >efficiently.
> >> 
> >> Many thanks and best wishes,
> >> Ranjan
> >> 
> >> ____________________________________________________________
> >> Publish your photos in seconds for FREE
> >> TRY IM TOOLPACK at http://www.imtoolpack.com/default.aspx?rc=if4
> >> 
> >> ______________________________________________
> >> R-help at 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/posting-guide.html
> >> and provide commented, minimal, self-contained, reproducible code.
> >>
> 
> ______________________________________________
> R-help at 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/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 


-- 
Important Notice: This mailbox is ignored: e-mails are set to be deleted on receipt. Please respond to the mailing list if appropriate. For those needing to send personal or professional e-mail, please use appropriate addresses.

____________________________________________________________
FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!



More information about the R-help mailing list