Skip to contents

Filter Vector Elements by Cumulative Mass Threshold

Usage

mass_filter(mass, thresh = 0.9)

Arguments

mass

A numeric vector of values (e.g., weights, similarities, or densities).

thresh

Numeric. Proportion of total cumulative mass to retain (default: 0.9).

Value

A numeric vector of the same length as `mass`, with low-contributing elements set to zero.

Details

Retains the top elements in a numeric vector based on cumulative mass. Elements contributing least to the total are zeroed out until the specified proportion of total mass (`thresh`) is retained.

This function normalizes the input to sum to 1, ranks the elements by size, and sets to zero those contributing below the cutoff needed to achieve `thresh` cumulative mass. Useful for sparsifying matrices.

Examples

x <- runif(10)
filtered <- mass_filter(x, thresh = 0.8)
print(filtered)
#>  [1] 0.0000000 0.0000000 0.5082687 0.4916758 0.0000000 0.6589923 0.6598774
#>  [8] 0.0000000 0.0000000 0.8191545