如何将稀疏矩阵转换为R中的矩阵?
矩阵" title="稀疏矩阵">稀疏矩阵是一种矩阵,其大多数元素等于零,但对零元素的数量没有限制。通常,非零元素的数量应等于行数或列数。要将稀疏矩阵转换为矩阵R,我们可以将as.matrix函数与稀疏矩阵对象名称一起使用。
例1
library(Matrix)输出结果i<−c(1,5,2,4,2,2,8);j<−c(2,5,3,2,4,2,4);x<−rpois(7,2)
M1<−sparseMatrix(i,j,x=x)
M1
8 x 5 sparse Matrix of class "dgCMatrix"
[1,] . 3 . . .[2,] . 5 3 3 .
[3,] . . . . .
[4,] . 2 . . .
[5,] . . . . 4
[6,] . . . . .
[7,] . . . . .
[8,] . . . 3 .
示例
M1<−as.matrix(M1)输出结果M1
[,1] [,2] [,3] [,4] [,5][1,] 0 3 0 0 0
[2,] 0 5 3 3 0
[3,] 0 0 0 0 0
[4,] 0 2 0 0 0
[5,] 0 0 0 0 4
[6,] 0 0 0 0 0
[7,] 0 0 0 0 0
[8,] 0 0 0 3 0
例2
i<−c(1,3,8,4,2,7,6,9,1,4,10);j<−c(2,5,3,2,4,2,4,5,2,7,3);x<−rpois(11,2)输出结果M2<−sparseMatrix(i,j,x=x)
M2
10 x 7 sparse Matrix of class "dgCMatrix"
[1,] . 3 . . . . .[2,] . . . 0 . . .
[3,] . . . . 3 . .
[4,] . 1 . . . . 3
[5,] . . . . . . .
[6,] . . . 0 . . .
[7,] . 4 . . . . .
[8,] . . 2 . . . .
[9,] . . . . 1 . .
[10,] . . 1 . . . .
示例
M2<−as.matrix(M2)输出结果M2
[,1] [,2] [,3] [,4] [,5] [,6] [,7][1,] 0 3 0 0 0 0 0
[2,] 0 0 0 0 0 0 0
[3,] 0 0 0 0 3 0 0
[4,] 0 1 0 0 0 0 3
[5,] 0 0 0 0 0 0 0
[6,] 0 0 0 0 0 0 0
[7,] 0 4 0 0 0 0 0
[8,] 0 0 2 0 0 0 0
[9,] 0 0 0 0 1 0 0
[10,] 0 0 1 0 0 0 0
范例3
i<−c(1,3,8,4,2,7,6,9,1,4,10);j<−c(2,5,3,2,4,2,4,5,2,7,3);x<−rpois(11,5)输出结果M3<−sparseMatrix(i,j,x=x)
M3
10 x 7 sparse Matrix of class "dgCMatrix"
[1,] . 8 . . . . .[2,] . . . 8 . . .
[3,] . . . . 3 . .
[4,] . 7 . . . . 7
[5,] . . . . . . .
[6,] . . . 8 . . .
[7,] . 3 . . . . .
[8,] . . 12 . . . .
[9,] . . . . 2 . .
[10,] . . 9 . . . .
示例
M3<−as.matrix(M3)输出结果M3
[,1] [,2] [,3] [,4] [,5] [,6] [,7][1,] 0 8 0 0 0 0 0
[2,] 0 0 0 8 0 0 0
[3,] 0 0 0 0 3 0 0
[4,] 0 7 0 0 0 0 7
[5,] 0 0 0 0 0 0 0
[6,] 0 0 0 8 0 0 0
[7,] 0 3 0 0 0 0 0
[8,] 0 0 12 0 0 0 0
[9,] 0 0 0 0 2 0 0
[10,] 0 0 9 0 0 0 0
例子4
i<−c(1,3,8,4,2,7,6,9,1,4,10,5,11,2,12);j<−c(2,5,3,8,6,2,4,2,4,5,2,7,3,2,1);x<−rpois(15,5)输出结果M4<−sparseMatrix(i,j,x=x)
M4
12 x 8 sparse Matrix of class "dgCMatrix"
[1,] . 3 . 3 . . . .[2,] . 6 . . . 6 . .
[3,] . . . . 2 . . .
[4,] . . . . 3 . . 6
[5,] . . . . . . 6 .
[6,] . . . 6 . . . .
[7,] . 5 . . . . . .
[8,] . . 4 . . . . .
[9,] . 2 . . . . . .
[10,] . 2 . . . . . .
[11,] . . 5 . . . . .
[12,] 6 . . . . . . .
示例
M4<−as.matrix(M4)输出结果M4
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8][1,] 0 3 0 3 0 0 0 0
[2,] 0 6 0 0 0 6 0 0
[3,] 0 0 0 0 2 0 0 0
[4,] 0 0 0 0 3 0 0 6
[5,] 0 0 0 0 0 0 6 0
[6,] 0 0 0 6 0 0 0 0
[7,] 0 5 0 0 0 0 0 0
[8,] 0 0 4 0 0 0 0 0
[9,] 0 2 0 0 0 0 0 0
[10,] 0 2 0 0 0 0 0 0
[11,] 0 0 5 0 0 0 0 0
[12,] 6 0 0 0 0 0 0 0
范例5
i<−c(1,3,8,4,2,7,6,9,1,4,10,5,11,2,12);j<−c(2,5,3,8,6,2,4,2,4,5,2,7,3,2,1);x<−round(rnorm(15),2)输出结果M5<−sparseMatrix(i,j,x=x)
M5
12 x 8 sparse Matrix of class "dgCMatrix"
[1,] . −0.65 . 0.38 . . . .[2,] . 0.93 . . . 0.56 . .
[3,] . . . . −1.02 . . .
[4,] . . . . −1.10 . . 0.35
[5,] . . . . . . 1.47 .
[6,] . . . −0.27 . . . .
[7,] . 1.31 . . . . . .
[8,] . . 0.23 . . . . .
[9,] . 1.47 . . . . . .
[10,] . 1.24 . . . . . .
[11,] . . −1.63 . . . . .
[12,] 0.92 . . . . . . .
示例
M5<−as.matrix(M5)输出结果M5
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8][1,] 0.00 −0.65 0.00 0.38 0.00 0.00 0.00 0.00
[2,] 0.00 0.93 0.00 0.00 0.00 0.56 0.00 0.00
[3,] 0.00 0.00 0.00 0.00 −1.02 0.00 0.00 0.00
[4,] 0.00 0.00 0.00 0.00 −1.10 0.00 0.00 0.35
[5,] 0.00 0.00 0.00 0.00 0.00 0.00 1.47 0.00
[6,] 0.00 0.00 0.00 −0.27 0.00 0.00 0.00 0.00
[7,] 0.00 1.31 0.00 0.00 0.00 0.00 0.00 0.00
[8,] 0.00 0.00 0.23 0.00 0.00 0.00 0.00 0.00
[9,] 0.00 1.47 0.00 0.00 0.00 0.00 0.00 0.00
[10,] 0.00 1.24 0.00 0.00 0.00 0.00 0.00 0.00
[11,] 0.00 0.00 −1.63 0.00 0.00 0.00 0.00 0.00
[12,] 0.92 0.00 0.00 0.00 0.00 0.00 0.00 0.00
以上是 如何将稀疏矩阵转换为R中的矩阵? 的全部内容, 来源链接: utcz.com/z/359999.html