Learn R Programming

cuadramelo (version 1.0.0)

make_non_negative: 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)

Value

A non-negative matrix, except if it is impossible to balance the matrix.

Arguments

Y

Matrix to be positivized.

allowSlack

Can colSums and rowSums be modified?

Examples

Run this code
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()

Run the code above in your browser using DataLab