View source: R/balance_matrix.R
make_non_negative | R Documentation |
Modifies as little as possible the entries of a matrix in order to make them non-negative, keeping row and column totals unchanged.
make_non_negative(Y, allowSlack = FALSE)
Y |
Matrix to be positivized. |
allowSlack |
Can colSums and rowSums be modified? |
A non-negative matrix, except if it is impossible to balance the matrix.
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()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.