[Rd] preserving sparse matrices (Matrix)

From: Tamas K Papp <tpapp_at_Princeton.EDU>
Date: Tue 19 Dec 2006 - 17:25:16 GMT


Hi,

I have sparse (tridiagonal) matrices, and I use the Matrix package for handling them. For certain computations, I need to either set the first row to zero, or double the last row. I find that neither operation preserves sparsity, eg

> m <- Diagonal(5)
> m
5 x 5 diagonal matrix of class "ddiMatrix"

     [,1] [,2] [,3] [,4] [,5]
[1,] 1 . . . .
[2,] . 1 . . .
[3,] . . 1 . .
[4,] . . . 1 .
[5,] . . . . 1
> m[1,] <- 0
> m
5 x 5 Matrix of class "dgeMatrix"

     [,1] [,2] [,3] [,4] [,5]
[1,] 0 0 0 0 0
[2,] 0 1 0 0 0
[3,] 0 0 1 0 0
[4,] 0 0 0 1 0
[5,] 0 0 0 0 1

> m <- Diagonal(5)
> rbind(m,1:5)
6 x 5 Matrix of class "dgeMatrix"

     [,1] [,2] [,3] [,4] [,5]
[1,] 1 0 0 0 0
[2,] 0 1 0 0 0
[3,] 0 0 1 0 0
[4,] 0 0 0 1 0
[5,] 0 0 0 0 1
[6,] 1 2 3 4 5

Currently, I am calling Matrix to make these sparse again, but my matrices are large (100x100). Is there a way to perform the operations I want without losing sparsity?

Thanks,

Tamas



R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Wed Dec 20 06:44:51 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 21 Dec 2006 - 02:31:04 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.