[R] Sparse matrix calculation

YONGWAN CHUN chun.49 at osu.edu
Wed Nov 15 19:22:19 CET 2006


Hello, 


I work on large matrices and found something interesting. For multiplication of matrices, the order has a huge influence on computing time when one of them is a sparse matrix. In the below example, M is a full matrix and A is a sparse matrix in a regular matrix class. A %*% M takes much more time than M %*% A; moreover, t(t(M) %*% t(A)) is much faster than A %*% M with same result. I would like to know how it is possible. Even though I do not have an exact reason, this fact may be helpful to others. 

Thanks, 

Yongwan


> n <- 1000
> M <- diag(n) - matrix(1/n,n,n)
> A <- matrix(rnorm(n*n)>2,n,n)
> system.time(M %*% A)
[1] 0.10 0.03 0.12   NA   NA
> system.time(A %*% M)
[1] 3.47 0.03 3.50   NA   NA
> system.time(t(t(M) %*% t(A)))
[1] 0.23 0.00 0.23   NA   NA



More information about the R-help mailing list