How to create a random binary matrix with equal number of ones in each column and equal number of 1 in each row ?

조회 수: 2 (최근 30일)
Hi All,
I want to create a random binary matrix with equal number of ones in each column and also equal number of ones in each row.
Appreciate if anyone have an idea to implement this in Matlab.
Thanks.
  댓글 수: 3
Raghwan Chitranshu
Raghwan Chitranshu 2019년 1월 13일
@Madhan thanks for the comment .
Basically I am looking to construct a matrix with these conditions
  1. no all 0 coulmns
  2. every column contains an odd number of 1's
  3. the number od 1's in each row of the matrix should be made equal or as close as possible to the average (total number of 1's in matrix divided by number of rows)
if you can help regarding this
Regards

댓글을 달려면 로그인하십시오.

답변 (2개)

Walter Roberson
Walter Roberson 2019년 1월 13일
N = 10; %number of 1s to place
M = 20; %dimension of matrix
maxtries = 10; %random placement can fail. How often to retry ?
for trynum = 1 : maxtries
L = zeros(M,M);
failed = false;
for K = 1 : M*N
cmask = sum(L) < N;
rmask = sum(L,2) < N;
locs = find(cmask & rmask & L ~= 1); %implicit expansion used!
if isempty(locs)
fprintf('try %d boxed in at iteration %d\n', trynum, K);
failed = true;
break;
end
thisloc = locs( randi(length(locs)) );
L(thisloc) = 1;
end
if ~failed
fprintf('try %d worked\n', trynum);
display(L)
break;
end
end
if failed
fprintf('Did not succeed in %d tries. You could increase maxtries, but are you sure there is a way to succeed?\n', maxtries);
end
My tests show that with this particular combination, 10 of 20, that success rate in placing within 10 tries is roughly 50% -- suggesting that maxtries should perhaps be increased.

Bruno Luong
Bruno Luong 2019년 1월 13일

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by