如何在 R 中将稀疏矩阵转换成矩阵?


稀疏矩阵是一种元素大部分为零的矩阵,但零元素的数量没有限制。作为一般标准,非零元素的数量有望等于行数或列数。若要将稀疏矩阵转换成 R 矩阵,我们可以将 as.matrix 函数与稀疏矩阵对象名结合使用。

Example1

 在线演示

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

Example2

 在线演示

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

Example3

 在线演示

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

Example4

 在线演示

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

Example5

 在线演示

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

更新于:2021-02-08

3K+ 次浏览

开启你的 职业生涯

完成课程,获得认证

立即开始
广告