Convert sparse matrix into dense matrix
Arguments
- x
A matrix.
- parallel
Logical value, default is
FALSE
. Setting to parallelize the computation with RcppParallel::setThreadOptions.- sparse
Logical value, default is
FALSE
, whether to output a sparse matrix.
Examples
m <- simulate_sparse_matrix(
1000, 1000,
decimal = 3
)
system.time(
a <- as.matrix(m)
)
#> user system elapsed
#> 0.002 0.002 0.003
system.time(
b <- as_matrix(m)
)
#> user system elapsed
#> 0.001 0.001 0.003
system.time(
c <- as_matrix(m, parallel = TRUE)
)
#> user system elapsed
#> 0.001 0.003 0.003
system.time(
d <- as_matrix(m, sparse = TRUE)
)
#> user system elapsed
#> 0.000 0.002 0.002
m[1:5, 1:5]
#> 5 x 5 sparse Matrix of class "dgCMatrix"
#> col_1 col_2 col_3 col_4 col_5
#> row_1 2.266 . . . .
#> row_2 . . . 2.467 .
#> row_3 . . . . .
#> row_4 . . . . .
#> row_5 . . . . .
a[1:5, 1:5]
#> col_1 col_2 col_3 col_4 col_5
#> row_1 2.266 0 0 0.000 0
#> row_2 0.000 0 0 2.467 0
#> row_3 0.000 0 0 0.000 0
#> row_4 0.000 0 0 0.000 0
#> row_5 0.000 0 0 0.000 0
b[1:5, 1:5]
#> col_1 col_2 col_3 col_4 col_5
#> row_1 2.266 0 0 0.000 0
#> row_2 0.000 0 0 2.467 0
#> row_3 0.000 0 0 0.000 0
#> row_4 0.000 0 0 0.000 0
#> row_5 0.000 0 0 0.000 0
c[1:5, 1:5]
#> col_1 col_2 col_3 col_4 col_5
#> row_1 2.266 0 0 0.000 0
#> row_2 0.000 0 0 2.467 0
#> row_3 0.000 0 0 0.000 0
#> row_4 0.000 0 0 0.000 0
#> row_5 0.000 0 0 0.000 0
identical(a, b)
#> [1] TRUE
identical(a, c)
#> [1] TRUE
identical(b, c)
#> [1] TRUE
identical(a, d)
#> [1] TRUE
identical(b, d)
#> [1] TRUE