Skip to content
Prev 257262 / 398506 Next

Avoiding loop

Hi:

Try the expm package. Using your example,
+ {
+        R = R%*%B
+ }
[,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 % ^ %
[,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
user  system elapsed
   0.02    0.00    0.01
+     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 <pmassicotte at hotmail.com> wrote: