简体   繁体   English

如何创建一个0和1的矩阵,使行和列的总和等于特定值?

[英]How to create a matrix of 0s and 1s such that rows and columns sum up to particular value?

I need to generate a matrix with n columns and m rows where each cell can be either 0 or 1 , such that sum of numbers in each column equals c and sum of numbers in each row equals r . 我需要生成一个具有n列和m行的矩阵,其中每个单元格可以为01 ,这样每列中的数字之和等于c ,每行中的数字之和等于r If it's not possible (most of the time probably), it should have columns sum to c+/-d and rows to r+/-d so that max(d over all rows and columns) is as small as possible. 如果不可能(可能大部分时间),则列总和应为c+/-d ,行应为r+/-d以使max(所有行和列的d)尽可能小。 In other words, every sum of a row should be close to r and every sum of a column should be close to c . 换句话说,行的每个和应接近r ,列的每个和应接近c

To illustrate what I'm looking for when no perfect solution exists, this solution: 为了说明当不存在完美解决方案时我在寻找什么,该解决方案:

1.1.1.......
1.1.1.......
...1..1...1.
...1.1..1...
....1...1..1

is better than this solution: 比这个解决方案更好:

1.1.1.......
1.1.1.......
...1..1...1.
...1.1..1...
....11..1...

Because the last row has a sum 0 (compared to 1) with the previous solution which is further away from the desired row sum of 2. 因为最后一行与先前的解决方案的总和为0(与1比较),所以与所需的行总和为2的距离更远。

Creating a matrix where it holds just for rows is easy - take permutations of a vector with exactly r 1s . 创建一个仅容纳行的矩阵很容易,可以对向量恰好为r 1s进行排列。 How to satisfy the second condition though? 但是如何满足第二个条件呢? Find a column with sum that's too high and another that's too low and swap some numbers? 查找总和过高而又太低的列并交换一些数字? Is that going to help, how long will it take, is it even going to terminate, if it's impossible to get a perfect result, when do I stop? 这是否有帮助,需要多长时间,甚至无法终止,如果不可能获得理想的结果,我什么时候停止? Is there a better way? 有没有更好的办法?

You can use pseudocode or your language of choice or just random blurbs. 您可以使用伪代码或您选择的语言,也可以只使用随机内容。

I've found Finding if binary matrix exists given the row and column sums which talks about answering whether it's possible or not and even constructing a solution if it is possible. 我发现在给定行和列总和的情况下查找二进制矩阵是否存在,这涉及到回答是否可能,甚至构造一个可能的解决方案。 If it isn't possible such solution will have really large ds though. 如果不可能的话,这种解决方案将具有很大的ds。

There is also this paper https://www.sciencedirect.com/science/article/pii/S0012365X06003980 which I don't really understand but it seems to also be concerned with constructing the perfect solutions 还有这篇论文https://www.sciencedirect.com/science/article/pii/S0012365X06003980我不是很了解,但它似乎也与构建完美的解决方案有关

This can be done via integer linear programming. 这可以通过整数线性编程来完成。

library(lpSolve)

# inputs
nr <- 3  # no of rows
nc <- 3  # no of cols
r <- 2   # sum of each row equals r
c <- 2   # sum of each col equals c

obj <- rep(1, nr * nc)
const.mat <- rbind(
 t(rep(1, nc) %x% diag(nr)),  # this matrix multiplied by c(m) is row sums
 (diag(nc) %x% t(rep(1, nr)))  # this matrix multiplied by c(m) is col sums
)
const.rhs <- c(rep(r, nr), rep(c, nc))
out <- lp(obj = obj, const.mat = const.mat, const.dir = "=", const.rhs = const.rhs, 
  all.bin = TRUE)

out
## Success: the objective function is 6 

matrix(out$solution, nr, nc)
##      [,1] [,2] [,3]
## [1,]    0    1    1
## [2,]    1    0    1
## [3,]    1    1    0

You could use the sample_degseq function from the igraph package for this, which creates a graph with given vertex degrees. 您可以为此使用igraph包中的sample_degseq函数,该igraph将创建具有给定顶点度的图。 The adjacency matrix of this graph will have your desired property. 此图的邻接矩阵将具有您想要的属性。

library(igraph)
row_col_sum = 3 
dim = 8
g = sample_degseq(rep(row_col_sum,dim), method = "simple.no.multiple")
as_adj(g)

Output: 输出:

8 x 8 sparse Matrix of class "dgCMatrix"

[1,] . . . . . 1 1 1
[2,] . . 1 1 1 . . .
[3,] . 1 . . 1 . 1 .
[4,] . 1 . . . 1 . 1
[5,] . 1 1 . . . 1 .
[6,] 1 . . 1 . . . 1
[7,] 1 . 1 . 1 . . .
[8,] 1 . . 1 . 1 . .

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何在 html canvas 和 javascript 上显示矩阵数组(我使用 1 和 0 创建的)? - How do i display a matrix array(that i created using 1s and 0s) on the html canvas with javascript? 如何从我的JSON obj中删除0和1 - How to remove 0s and 1s from my JSON obj 从字符串转换为0和1,以及0和1转换为字符串的有效方法? - Efficient way to convert from string to 0s and 1s, and 0s and 1s to string? 如何将文件的二进制数据作为 1 和 0 的字符串获取? - How do I get the binary data of a file as a string of 1s and 0s? 使用 Javascript,如何将二进制字符串(1 和 0)转换回 Unicode 字符? - Using Javascript, how do I convert a binary string (1s and 0s) back into a Unicode character? HTML5 FileReader-上传CSV文件并将其内容显示为原始二进制文件(0和1的字符串) - HTML5 FileReader - upload a CSV file and display its content as raw binary (strings of 0s and 1s) 查找行号布尔矩阵最大数 1s - find row number boolean matrix maximum number 1s 如何在Javascript中添加前面带有0的数字? - How to add numbers with 0s in front in Javascript? 当heatMap boxOnClick时,从dataTable中筛选出带有0的行 - filter out rows with 0s from dataTable when heatMap boxOnClick 如何修复 Android Cordova 应用程序在后台更改所有 setIntervals 低于 1s 到 1s? - How to fix that an Android Cordova App in background changes all setIntervals below 1s upto 1s?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM