[R] A faster way to calculate Trace?

YONGWAN CHUN chun.49 at osu.edu
Fri Oct 27 01:38:39 CEST 2006


I want to know how to get trace of product of matrices **faster** when the matrices are really big. Unfortunately the matrices are not symmetric. If anybody know how to get the trace of it, please help me. An example is as below.

n <- 2500
a <- matrix(rnorm(n*n),n,n)
b <- matrix(rnorm(n*n),n,n)
tr1 <- sum(diag(a %*% b))
tr2 <- sum(diag(a %*% b %*% a %*% b))

Thanks,

Yongwan Chun



More information about the R-help mailing list