I want to construct a binary(0s and 1s) matrix satisfying the following constraints:
-
Each column must contains only single binary 1 and rest elements of that column are 0s.
-
The sum of each ROW of matrix should be a desired value.
For example, given arowSumvector of [5 7 6 8 …….] then the sum of first row should be 5, sum of second row should be 7 and so on. -
nCol==Sum(rowSum)
Moreover, I would like to consider several (e.g., 7) matrices satisfying the same conditions.
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
nRowsxnColswithrowSum(a vector of lengthnRows)rowSum(k)the number of1s inkth row.So, if
nCol ~= sum( rowSum )it would be impossible to construct such matrix: you would either have columns with no1s, or columns with too many1s…Therefore, your binary matrix is completely defined through
rowSum– up to random permutation of its columns.How about this function to construct the basic matrix
b:Now you can use
randpermto randomly permute the order of the columns:Good luck with your thesis.