Skip to contents

Sparsify a Dense Matrix by Cumulative Mass Filtering

Usage

mat_sparsify(mat, row_mass = 0.9, col_mass = 0.9)

Arguments

mat

A numeric matrix.

row_mass

Proportion of row-wise cumulative mass to retain (default: 0.9).

col_mass

Proportion of column-wise cumulative mass to retain (default: 0.9).

Value

A sparsified matrix of the same shape with many zero entries.

Details

Applies mass-based thresholding to rows and columns to keep only the most informative values. For each row/column, entries are retained until the cumulative sum reaches a specified mass (e.g., 90

Examples

mat <- matrix(runif(100), nrow = 10)
mat_sparse <- mat_sparsify(mat, row_mass = 0.8, col_mass = 0.8)
print(mat_sparse)
#>            [,1]      [,2]      [,3]      [,4]      [,5]      [,6]      [,7]
#>  [1,] 0.0000000 0.0000000 0.0000000 0.9009332 0.8448093 0.0000000 0.0000000
#>  [2,] 0.0000000 0.0000000 0.9587667 0.8515347 0.7761037 0.9785804 0.9982637
#>  [3,] 0.8481066 0.0000000 0.9668923 0.0000000 0.0000000 0.0000000 0.7109441
#>  [4,] 0.8759437 0.9256773 0.0000000 0.0000000 0.0000000 0.9951824 0.9162931
#>  [5,] 0.0000000 0.7904869 0.8850977 0.0000000 0.0000000 0.0000000 0.7834048
#>  [6,] 0.8993639 0.0000000 0.8600141 0.8267845 0.0000000 0.0000000 0.0000000
#>  [7,] 0.0000000 0.0000000 0.8572805 0.0000000 0.0000000 0.0000000 0.6555618
#>  [8,] 0.8229108 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
#>  [9,] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
#> [10,] 0.0000000 0.0000000 0.0000000 0.0000000 0.7919232 0.7159001 0.6408825
#>            [,8]      [,9]     [,10]
#>  [1,] 0.0000000 0.7280732 0.9948426
#>  [2,] 0.0000000 0.0000000 0.0000000
#>  [3,] 0.0000000 0.0000000 0.0000000
#>  [4,] 0.7027963 0.0000000 0.0000000
#>  [5,] 0.0000000 0.7480587 0.9887503
#>  [6,] 0.0000000 0.0000000 0.0000000
#>  [7,] 0.7381451 0.0000000 0.0000000
#>  [8,] 0.0000000 0.0000000 0.0000000
#>  [9,] 0.0000000 0.0000000 0.0000000
#> [10,] 0.0000000 0.8746683 0.0000000