I think this works in general, although it's a little bit clunky:
id_y <- array(1:10,dim=c(2,1,5)) id_yt <- aperm(id_y,c(2,1,3)) m_id <- array(dim=c(dim(id_y)[1],dim(id_y)[1],dim(id_y)[3])) for (i in 1:dim(id_y)[3]){ m1 <- array(id_y[,,i],dim=dim(id_y)[1:2]) m2 <- array(id_yt[,,i],dim=dim(id_yt)[1:2]) m_id[,,i] <- m1 %*% m2 } m_id There may also be a clever way to do this with the tensor() function, in the tensor package, but I haven't found the right permutation yet ... Patrick Burns wrote: > > You need to put in calls to 'as.matrix'. It's > a bit tricky though -- what you want depends > on whether it is the first or second subscript > that has length 1. > > as.matrix(id_y[,,i]) > > if the second dimension has length 1. > > t(as.matrix(id_y[,,i])) > > if the first dimension has length 1. > > > Patrick Burns > patr...@burns-stat.com > +44 (0)20 8525 0696 > http://www.burns-stat.com > (home of "The R Inferno" and "A Guide for the Unwilling S User") > > MarcioRibeiro wrote: >> Hi again, >> I understood what you guys explained... >> But, there isn't a way to do a multiplication of matrix with a FOR >> command >> or otherelse where one o my dimension is ONE... >> Well, as my data file is small, I did the procedure at the excel... But, >> this is not the good procedure... >> Thanks, >> Marcio >> >> >> Uwe Ligges-3 wrote: >> >>> >>> MarcioRibeiro wrote: >>> >>>> Hi listers, >>>> I am having some trouble in a matrix multiplication... >>>> I have already checked some posts, but I didn't find my problem... >>>> I have the following code... >>>> But I am not getting the right multiplication... >>>> I checked the dimension and they are fine... >>>> >>>> id_y <- array(1:10,dim=c(2,1,5)) >>>> id_yt<-aperm(id_y,c(2,1,3)) >>>> m_id<-array(dim=c(dim(id_y)[1],dim(id_y)[1],dim(id_y)[3])) >>>> for (i in 1:dim(id_y)[3]){ >>>> m_id[,,i]<-id_y[,,i]%*%id_yt[,,i] >>>> } >>>> m_id >>>> >>>> >>>> >>> Due to indexing, you are loosing dimensions. >>> The problem is that you expect to *loose exactly one* dimension in this >>> case (hence drop = FALSE won't be of help right away). Hence it might be >>> a good idea to know what you are going to have exactly at the end (as >>> in- and output). >>> >>> Uwe Ligges >>> >>> ______________________________________________ >>> R-help@r-project.org 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. >>> >>> >>> >> >> > > ______________________________________________ > R-help@r-project.org 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. > > -- View this message in context: http://www.nabble.com/Matrix-multiplication---code-problem-tp22835003p22853920.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help@r-project.org 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.