简体   繁体   English

MATLAB:如何生成每行和每列具有特定数量 1 的随机二进制矩阵?

[英]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 ,矩阵的每一列上的唯一数J1 s,矩阵的每一行上的唯一数K1 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 * 10J = 3K = 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.

相关问题 MATLAB:生成二进制矩阵的所有可能组合,每列中只有一个“ 1” - MATLAB: Generate all possible combination of a binary matrix with only one '1' in each column 如何在矩阵中找到最大数量为 1 的行 - How to find the row with maximum number of 1s in a matrix MATLAB计算与矩阵中每个列号相对应的平均值? - MATLAB compute average value corresponding to each column number in matrix? 如何在JavaScript中为数组中的每个对象分配一个随机数? - How can I assign a random number to each object in an array in javascript? 2D数组-我想查找每行中哪些列中包含1 - 2D array - I want to find which columns have 1s in them in each row 我如何在R中的分割字符串变量中访问每一行中的特定元素编号 - How can I access an specific element number in each row in a splited string variable in R 如何在Matlab矩阵的每一行中删除多余的重复元素? - How to remove extra duplicated elements in each row of a matrix in matlab? 分别查看矩阵中的每一行(Matlab) - Look at each row separately in a matrix (Matlab) 如何使用Numpy读取具有二进制值的文本文件,其中每一行本身都是二进制向量? - How can I read a text file with binary values, where each row is a binary vector itself using Numpy? 如何获得仅由 0 和 1 组成的 3D 矩阵的 3D 坐标 - How can I get 3D coordinates of a 3D matrix consisting of only 0s and 1s
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM