I create a matrix using r as follows:
Assignment <- matrix(rep(0,50), nrow = 10, ncol = 5)
I would like to randomly fill this matrix with 20 ones such that every row has 2 ones and every column has 4 ones.
I tried looping through row by row, keeping track of how often a 1 is put in the columns. This gives me correct row sums, but the column sums are not equally distributed.
How can one achieve this?
I found that your strategy works if you just tell it to retry if the column sums aren't right, until it succeeds. Since the matrix is so small, this only takes about a second to run (on my machine).
generate_matrix <- function() {
rows <- 10
cols <- 5
target_row_ones <- 2
target_col_ones <- 4
# Initialize the matrix with zeros
matrix <- matrix(0, nrow = rows, ncol = cols)
# Track the number of ones in each column
col_count <- rep(0, cols)
for (row in 1:rows) {
# Identify columns that can accept more ones
available_cols <- which(col_count < target_col_ones)
# Randomly select 2 columns for the current row
selected_cols <- sample(available_cols, target_row_ones)
# Place ones in the selected columns and update column counts
matrix[row, selected_cols] <- 1
col_count[selected_cols] <- col_count[selected_cols] + 1
}
# Verify column constraints
if (all(col_count == target_col_ones)) {
return(matrix)
} else {
# Retry if constraints are not satisfied
return(generate_matrix())
}
}
# Generate and print the matrix
Assignment <- generate_matrix()
print(Assignment)