[英]MATLAB : How can I generate a random binary matrix with a specific number of 1s in each row and in each column?
I would like to generate a binary matrix A
of dimension m * n
with a unique number J
of 1
s on each column of the matrix and a unique number K
of 1
s on each row of the matrix.我想生成一个维度为m * n
的二进制矩阵A
,矩阵的每一列上的唯一数J
为1
s,矩阵的每一行上的唯一数K
为1
s。
For example, for a binary matrix A
of dimension m * n = 5 * 10
with J = 3
and K = 6
we may obtain the following matrix:例如,对于维度为m * n = 5 * 10
且J = 3
和K = 6
的二进制矩阵A
,我们可以获得以下矩阵:
1 0 1 1 1 0 1 1 0 0
0 1 1 0 0 1 1 0 1 1
1 1 0 1 1 1 0 1 0 0
0 1 1 0 1 0 1 0 1 1
1 0 0 1 0 1 0 1 1 1
This answer by Luis Mendo , gives just the specific number of 1
s on each column. Luis Mendo 的这个答案只给出了每列1
的具体数量。 In my case I'm trying to add the option of the specific factor of 1
s on each column and on each row, which can be a different number.在我的情况下,我试图在每一列和每一行上添加特定因子1
的选项,这可以是不同的数字。
How can I construct this matrix?我怎样才能构造这个矩阵?
I made this just for fun.我做这个只是为了好玩。 It can get stuck, so it doesn't work all the time.它可能会卡住,所以它不会一直工作。 For the values in your example, the function found a matrix in about 40 % of the runs.对于您示例中的值,function 在大约 40% 的运行中发现了一个矩阵。 You might want to tinker a bit with max_iterations.您可能想稍微修改一下 max_iterations。 If it's too small, the probability of finding a matrix goes down, but if it's too large it's really time consuming.如果它太小,找到矩阵的概率就会下降,但如果它太大,它真的很耗时。
Note: I wrote this in python and haven't run it in matlab, so there might be typos.注意:我在 python 中写了这个,并没有在 matlab 中运行它,所以可能有错别字。
function A = generate_matrix(m,n,J,K, max_iterations)
if nargin < 5
max_iterations = m*K*4;
end
% Check if it's possible to create matrix:
if (m*K ~= n*J)
error('Matrix not possible')
end
A = falses(m,n);
allowed_rows = 1:m;
allowed_cols = 1:n;
too_many_iterations = false;
counter = 0;
while ~isempty(allowed_rows) && ~too_many_iterations
% pick random row and column from the ones that don't have enough ones
row_index = allowed_rows(randi(length(allowed_rows)));
col_index = allowed_cols(randi(length(allowed_cols)));
A(row_index, col_index) = true;
% Update allowed_rows and allowed_cols
allowed_rows = find(sum(A,2) < K);
allowed_cols = find(sum(A,1) < J);
% Keep track of number of iterations
counter = counter + 1;
too_many_iterations = counter > max_iterations;
end
if ~isempty(allowed_rows)
warning('Matrix not found!')
A = [];
end
end
The function will start with a matrix of zeros and randomly pick a row and column that doesn't have enough ones and place a one at that location, then pick a new row and column and so on until there are no rows left. function 将从零矩阵开始,随机选择一个没有足够的行和列并在该位置放置一个,然后选择一个新的行和列,依此类推,直到没有剩下的行。 The reason it doesn't work all the time, is that it can reach a dead end.它不能一直工作的原因是它可能会走到死胡同。 For example, with m=3, n=6, J=2 and K=4, the following matrix is a dead end, since the only row without four ones is row 1, the only column without two ones is column 3, but A[1,3] already is one.例如,在 m=3、n=6、J=2 和 K=4 的情况下,下面的矩阵是一个死胡同,因为唯一没有四个 1 的行是第 1 行,唯一没有两个 1 的列是第 3 列,但是A[1,3] 已经是一。
[1, 0, 1, 1, 0, 0;
1, 1, 0, 0, 1, 1;
0, 1, 0, 1, 1, 1]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.