[Rd] preserving sparse matrices (Matrix)

Tamas K Papp tpapp at Princeton.EDU
Tue Dec 19 18:25:16 CET 2006


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



More information about the R-devel mailing list