简体   繁体   English

构造一个二进制矩阵,使得每一列仅包含单个“ 1”,并且每一行的总和具有期望的值

[英]construct a binary matrix such that each column contains only single “1” and sum of each row is of desired value

I want to construct a binary(0s and 1s) matrix satisfying the following constraints: 我想构造一个满足以下约束的二进制(0s和1s)矩阵:

  1. Each column must contains only single binary 1 and rest elements of that column are 0s. 每列必须仅包含单个二进制1,并且该列的其余元素为0。

  2. The sum of each ROW of matrix should be a desired value. 矩阵的每个ROW的总和应为所需值。 For example, given a rowSum vector of [5 7 6 8 .......] then the sum of first row should be 5, sum of second row should be 7 and so on. 例如,给定rowSum向量为[5 7 6 8 .......],则第一行的总和应为5,第二行的总和应为7,依此类推。

  3. nCol == Sum(rowSum) nCol == Sum(rowSum)

Moreover, I would like to consider several (eg, 7) matrices satisfying the same conditions. 此外,我想考虑几个 (例如7个)满足相同条件的矩阵。

EDIT: 编辑:
I have tried to write the code and completed the one part of it. 我试图编写代码并完成其中一部分。 The code is: 代码是:

x=rand(21,50,7);
for k=1:7
    cons=max(x(:,:,7));
    for i=1:50
        for j=1:21
            if x(j,i,k)==cons(i)
                x(j,i,k)=1;
            else
                x(j,i,k)=0;
            end
        end
     end
 end
 x

It would not always be possible to construct a binary matrix that satisfies your requirements. 构造满足您要求的二进制矩阵并不总是可能的。 Suppose you want a binary matrix of size nRows x nCols with rowSum (a vector of length nRows ) rowSum(k) the number of 1 s in k th row. 假设您想要一个大小为nRows x nCols的二进制矩阵,其中rowSum (长度为nRows的向量) rowSum(k)在第k行中的个数为1 s。 So, if nCol ~= sum( rowSum ) it would be impossible to construct such matrix: you would either have columns with no 1 s, or columns with too many 1 s... 因此,如果nCol ~= sum( rowSum )则不可能构造这样的矩阵:您将拥有无1 s的列,或具有过多1 s的列...

Therefore, your binary matrix is completely defined through rowSum - up to random permutation of its columns. 因此,您的二进制矩阵是通过rowSum完全定义的-直至其列的随机排列。

How about this function to construct the basic matrix b : 这个函数如何构造基本矩阵b

function b = makeBizarreBinaryMatrix( rowSum )

nRows = numel( rowSum );
nCols = sum( rowSum );   
rows = 1:nRows;
rowInd = zeros( 1, nCols );
rowInd( cumsum( [1 rowSum(1:end-1)] ) ) = 1;
rowInd = rows( cumsum( rowInd ) );
b = sparse( rowInd, 1:nCols, 1, nRows, nCols );

Now you can use randperm to randomly permute the order of the columns: 现在,您可以使用randperm随机排列列的顺序:

nb = b(:, randperm(size(b,2)) );

Good luck with your thesis. 祝你好运。

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

相关问题 为矩阵的每一行设置特定列的值 - Set a value of a specific column for each row of a matrix 每行乘积的总和 - Sum of product each row by a matrix 如何生成随机二进制矩阵,其中每行中只有一位的值为1? - How to generate a random binary matrix where only one bit in each row has value 1? 将矩阵中的每一列与另一列中的对应行相乘,然后在Matlab中求和 - Multiply each column in a matrix by corresponding row in another and sum results in Matlab 如何将矩阵A的每一列乘以矩阵B的每一行,并在Matlab中求和矩阵? - How to multiply each column of matrix A by each row of matrix B and sum resulting matrices in Matlab? 为矩阵的每一列创建向量,并构造矩阵,但删除该列 - Create vector for each column of a matrix and also construct the matrix but with that column removed MATLAB:如何生成每行和每列具有特定数量 1 的随机二进制矩阵? - MATLAB : How can I generate a random binary matrix with a specific number of 1s in each row and in each column? 表示二进制矩阵中每列随机化的图形 - graph representing the randomization of each column in a binary matrix 矩阵各列的平均值 - Mean value of each column of a matrix 在 Matlab 中构造一个矩阵,跟踪每行中的相等元素 - Construct a matrix in Matlab that keeps track of equal elements in each row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM