[R] column permutation of sparse matrix

khai ktluong71 at gmail.com
Tue Dec 20 06:26:55 CET 2011


Hi,

I'm very new to working with sparse matrices and would like to know how I
can column permute a sparse matrix. Here is a small example: 

> M1 <-
> spMatrix(nrow=5,ncol=6,i=sample(5,15,replace=TRUE),j=sample(6,15,replace=TRUE),x=round_any(rnorm(15,2),0.001))
> M1
5 x 6 sparse Matrix of class "dgTMatrix"

[1,] 2.983      .     1.656    5.003    .        .
[2,]    .            .     2.990        .         .        .
[3,]    .     0.592   5.349    1.115    .        .
[4,] 1.836      .     2.804        .         .        .
[5,]    .     6.961       .             .         .     1.077

I know I can permute entire columns this way

> M1[,sample(6,6)]
5 x 6 sparse Matrix of class "dgTMatrix"

[1,] 5.003        .           .           .       1.656    2.983
[2,]     .             .           .           .       2.990       .
[3,] 1.115    0.592      .           .       5.349       .
[4,]     .             .           .          .       2.804     1.836
[5,]     .         6.961   1.077     .          .            .

But I would like the new sparse matrix to look like this...where only the
nonzero elements are permuted.

[1,] 1.656      .     5.003    2.983    .        .
[2,]    .            .     2.990        .         .        .
[3,]    .    5.349    1.115    0.592    .        .
[4,] 2.804      .     1.836        .         .        .
[5,]    .     1.077       .             .         .     6.961

Thanks in advance for any advice!

--
View this message in context: http://r.789695.n4.nabble.com/column-permutation-of-sparse-matrix-tp4216726p4216726.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list