This function takes a matrix as input and partitions each column such that only one entry in each row is non-zero, and the sums of each row are roughly equivalent.

sum_preserving_matrix_partition(X, option = 2, tol = 0.001)

Arguments

X

A matrix of size p x k

option

An integer indicating whether to allow +/- values (option 1) or only + values (option 2). Default is 1.

tol

A numeric value indicating the tolerance for the row sums. Default is 0.1.

Value

A matrix with segmented rows

Details

The function uses a greedy algorithm to select the entry with the maximum absolute value (or maximum positive value) in each column. The rows are then normalized to ensure that the sums are roughly equivalent.

Examples

X <- matrix(rnorm(3000), nrow = 1000)
Y <- sum_preserving_matrix_partition(X, option = 1)
Y <- sum_preserving_matrix_partition(X, option = 2)