
Extract Sparse Matrix Notation from a Dense Matrix
Source:R/index_matrices.R
sparse_matrix_notation.RdConverts a dense matrix to a sparse representation by extracting its non-zero
entries and their indices. Entries with absolute value less than or equal to
tol are treated as zeros.
Arguments
- M
A numeric matrix or object coercible to a matrix.
- zero_based
Logical; if
TRUE(default), the returned row and column indices are zero-based (starting at 0). IfFALSE, indices are one-based (as in standard R matrices).- tol
Numeric tolerance (default
1e-4). Entries with absolute value less than or equal totolare treated as zero.
Value
A named list with components:
row_indexInteger vector of row indices for non-zero entries (adjusted by
zero_based).col_indexInteger vector of column indices for non-zero entries (adjusted by
zero_based).valuesNumeric vector of the non-zero entries.
MThe original input matrix, coerced to a dense matrix.
MsparseA copy of
Mwith near-zero entries (as determined bytol) replaced by exact zeros.
Examples
M <- matrix(c(5, 0, 0,
0, 0, 3,
0, 2, 0), nrow = 3, byrow = TRUE)
sparse_matrix_notation(M)
#> $row_index
#> [1] 0 1 2
#>
#> $col_index
#> [1] 0 2 1
#>
#> $values
#> [1] 5 3 2
#>
#> $M
#> [,1] [,2] [,3]
#> [1,] 5 0 0
#> [2,] 0 0 3
#> [3,] 0 2 0
#>
#> $Msparse
#> [,1] [,2] [,3]
#> [1,] 5 0 0
#> [2,] 0 0 3
#> [3,] 0 2 0
#>