Utility function to extract row and column indices of the non-zero elements of a sparse matrix.

Matrix.to.Coord(M, index1 = TRUE)

Arguments

M

A matrix that is a subclass of sparseMatrix, as defined in the Matrix package.

index1

TRUE if the index of the first element should be 1, and FALSE if 0.

Value

A list with two named elements.

rows

Integer vector containing row indices of non-zero elements

cols

Integer vector containing column indices of non-zero elements

Details

A wrapper to Matrix.to.Pointers for order='triplet' and values=FALSE, for extracting the row and column indices of a sparsity pattern from a matrix that has that same pattern.

Examples

M1 <- as(kronecker(diag(3), matrix(TRUE,2,2)),"sparseMatrix")
C <- Matrix.to.Coord(M1)
M2 <- Matrix::sparseMatrix(i=C$rows, j=C$cols)
all.equal(M1,M2)
#> [1] "Attributes: < Length mismatch: comparison on first 6 components >"
#> [2] "Attributes: < Component “class”: 1 string mismatch >"             
#> [3] "Attributes: < Component “i”: Numeric: lengths (9, 12) differ >"   
#> [4] "Attributes: < Component “p”: Mean relative difference: 0.4 >"