It is not always possible to build a binary matrix that satisfies your requirements. Suppose you need a binary matrix of size nRows x nCols with rowSum (a vector of length nRows ) rowSum(k) number 1 in k th row. So, if nCol ~= sum( rowSum ) it would be impossible to build such a matrix: you would have columns without 1 s or columns with too many 1 s ...
Therefore, your binary matrix is ββcompletely determined through rowSum - until its columns are randomly rearranged.
How about this function to build the base 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 rearrange the column order:
nb = b(:, randperm(size(b,2)) );
Good luck with your dissertation.
source share