2016-03-19 215 views
1

我如何使用稀疏矩阵命令R(矩阵封装),构建以下稀疏矩阵命令

[1,] 1.002 0.210 0.002 .  .  .  2.943 0.051 
[2,] 0.210 1.002 0.210 .  .  .  7.515 2.943 
[3,] 0.002 0.210 1.002 .  .  .  0.843 7.515 
[4,] .  .  .  16.003 3.354 0.031 18.691 1.122 
[5,] .  .  .  3.354 16.003 3.354 13.675 18.691 
[6,] .  .  .  0.031 3.354 16.003 0.440 13.675 
[7,] 2.943 7.515 0.843 18.691 13.675 0.440 109.002 22.848 
[8,] 0.051 2.943 7.515 1.122 18.691 13.675 22.848 109.002 

矩阵我还提供了dput命令能够将矩阵复制到您的控制台

new("dsCMatrix" 
    , i = c(0L, 0L, 1L, 0L, 1L, 2L, 3L, 3L, 4L, 3L, 4L, 5L, 0L, 1L, 2L, 
3L, 4L, 5L, 6L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L) 
    , p = c(0L, 1L, 3L, 6L, 7L, 9L, 12L, 19L, 27L) 
    , Dim = c(8L, 8L) 
    , Dimnames = list(NULL, NULL) 
    , x = c(1.002, 0.21, 1.002, 0.002, 0.21, 1.002, 16.003, 3.354, 16.003, 
0.031, 3.354, 16.003, 2.943, 7.515, 0.843, 18.691, 13.675, 0.44, 
109.002, 0.051, 2.943, 7.515, 1.122, 18.691, 13.675, 22.848, 
109.002) 
    , uplo = "U" 
    , factors = list() 
) 
+0

您需要以提供行,列索引和'x'或值 – akrun

+0

@akrun我无法理解命令需要​​的非零元素的确切结构 – raK1

+0

您必须将rowindex作为向量传递,例如第一个元素1.002是row1,同一列上的第二个元素是0.210,所以'sparseMatrix(c(1,2,..),c(1,1,...),x = c(1.002,0.210,...) )' – akrun

回答

1

你有一个CsparseMatrix,他们没有列索引,但它很容易强制TsparseMatrix有它们。 i和j参数(在x值内部存在的值)是基于0而不是1,因此如果您想要用他们来解决某些问题,您需要添加1.

CSM <- new("dsCMatrix" 
    , i = c(0L, 0L, 1L, 0L, 1L, 2L, 3L, 3L, 4L, 3L, 4L, 5L, 0L, 1L, 2L, 
3L, 4L, 5L, 6L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L) 
    , p = c(0L, 1L, 3L, 6L, 7L, 9L, 12L, 19L, 27L) 
    , Dim = c(8L, 8L) 
    , Dimnames = list(NULL, NULL) 
    , x = c(1.002, 0.21, 1.002, 0.002, 0.21, 1.002, 16.003, 3.354, 16.003, 
0.031, 3.354, 16.003, 2.943, 7.515, 0.843, 18.691, 13.675, 0.44, 
109.002, 0.051, 2.943, 7.515, 1.122, 18.691, 13.675, 22.848, 
109.002) 
    , uplo = "U" 
    , factors = list() 
) 
TSM <-as(CSM, "TsparseMatrix") 
[email protected] 
# [1] 0 0 1 0 1 2 3 3 4 3 4 5 0 1 2 3 4 5 6 0 1 2 3 4 5 6 7 
[email protected] 
# [1] 0 1 1 2 2 2 3 4 4 5 5 5 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 
[email protected] +1 
# [1] 1 2 2 3 3 3 4 5 5 6 6 6 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 
[email protected] +1 
# [1] 1 1 2 1 2 3 4 4 5 4 5 6 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8