
Extract Sparse Matrix Notation from a Dense Matrix
Source:R/index_matrices.R
sparse_matrix_notation.Rd
Converts 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 totol
are treated as zero.
Value
A named list with components:
row_index
Integer vector of row indices for non-zero entries (adjusted by
zero_based
).col_index
Integer vector of column indices for non-zero entries (adjusted by
zero_based
).values
Numeric vector of the non-zero entries.
M
The original input matrix, coerced to a dense matrix.
Msparse
A copy of
M
with 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
#>