Hi: Try the expm package. Using your example,
> R = A%*%B > for(i in 1:100) + { + R = R%*%B + } > R [,1] [,2] [,3] [,4] [,5] [1,] 9.934879e+47 1.098761e+48 8.868476e+47 7.071831e+47 6.071370e+47 [2,] 1.492692e+48 1.650862e+48 1.332468e+48 1.062526e+48 9.122090e+47 [3,] 6.693145e+47 7.402373e+47 5.974708e+47 4.764305e+47 4.090293e+47 [4,] 5.895689e+47 6.520416e+47 5.262850e+47 4.196661e+47 3.602954e+47 [5,] 8.347321e+47 9.231830e+47 7.451326e+47 5.941778e+47 5.101187e+47 library(expm) # The matrix power function is an operator % ^ % > A %*% (B %^% 101) [,1] [,2] [,3] [,4] [,5] [1,] 9.934879e+47 1.098761e+48 8.868476e+47 7.071831e+47 6.071370e+47 [2,] 1.492692e+48 1.650862e+48 1.332468e+48 1.062526e+48 9.122090e+47 [3,] 6.693145e+47 7.402373e+47 5.974708e+47 4.764305e+47 4.090293e+47 [4,] 5.895689e+47 6.520416e+47 5.262850e+47 4.196661e+47 3.602954e+47 [5,] 8.347321e+47 9.231830e+47 7.451326e+47 5.941778e+47 5.101187e+47 > system.time(replicate(1000, A %*% (B %^% 101))) user system elapsed 0.02 0.00 0.01 > system.time(replicate(1000, {R = A%*%B + for(i in 1:100) + { + R = R%*%B + } })) user system elapsed 0.15 0.00 0.15 HTH, Dennis On Mon, Apr 18, 2011 at 9:06 AM, Filoche <pmassico...@hotmail.com> wrote: > Hi everyone. > > I'm using matrix product such as : > > > #Generate some data > NCols = 5 > NRows = 5 > A = matrix(runif(NCols*NRows), ncol=NCols) > B = matrix(runif(NCols*NRows), ncol=NCols) > > #First calculation > R = A%*%B > > > for(i in 1:100) > { > R = R%*%B > } > > > I would like to know if it was possible to avoid the loop by using something > like mapply or anything else. > > Tx in advance, > Phil > > -- > View this message in context: > http://r.789695.n4.nabble.com/Avoiding-loop-tp3457963p3457963.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. > ______________________________________________ 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.