4
votes

I want to construct a binary(0s and 1s) matrix satisfying the following constraints:

  1. Each column must contains only single binary 1 and rest elements of that column are 0s.

  2. The sum of each ROW of matrix should be a desired value. 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.

  3. 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
1
It's unethical to write your thesis code for you; it may (according to the rules of your institution) be unethical for you to ask us to write your code for you. You'll find, though, that SO loves a trier, so write as much of your code as you can and ask again for help in ironing out the bugs.High Performance Mark
@HighPerformanceMark: It depends very much on the part of the thesis. If this was to become the core algorithm of a thesis titled "an algorithm for distributing numbers in an array", then yes, it's unethical. If instead, the question relates to, say, creating code to simulate input for testing the analysis code the thesis is about, it's perfectly fine to ask for help.Jonas
..I have tried to write the code and completed the one part of it 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 Now i am trying to implement other condition on it.Your little effort will be the great help to me..user1987182
@VijaySaini I've embedded your code in your question. Please don't post code in comment in the future.Eitan T
@VijaySaini - what is the status with this question? are you happy with the answer you got?Shai

1 Answers

6
votes

It would not always be possible to construct a binary matrix that satisfies your requirements. Suppose you want a binary matrix of size nRowsxnCols with rowSum (a vector of length nRows) rowSum(k) the number of 1s in kth row. So, if nCol ~= sum( rowSum ) it would be impossible to construct such matrix: you would either have columns with no 1s, or columns with too many 1s...

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:

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:

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

Good luck with your thesis.