make_non_negative {cuadramelo} | R Documentation |
Make non-negative
Description
Modifies as little as possible the entries of a matrix in order to make them non-negative, keeping row and column totals unchanged.
Usage
make_non_negative(Y, allowSlack = FALSE)
Arguments
Y |
Matrix to be positivized. |
allowSlack |
Can colSums and rowSums be modified? |
Value
A non-negative matrix, except if it is impossible to balance the matrix.
Examples
Y <- c(1,2,-1,1,
2,2,3,1,
1,1,-2,3) |>
matrix(nrow = 3)
X <- make_non_negative(Y)
Y
X |> round(2)
rowSums(Y)
rowSums(X)
colSums(Y)
colSums(X)
set.seed(2)
Y <- rnorm(3*5) |> matrix(3,5) |> round(3)
Y
tryCatch(make_non_negative(Y), error = function(e) {
print(e)
})
make_non_negative(Y, allowSlack = TRUE) |> round()
[Package cuadramelo version 1.0.0 Index]