how to generate a RANDOM matrix of 0's and 1's such that the number of 1's in any column is the same.
2 次查看(过去 30 天)
显示 更早的评论
The positioning of 0's and 1's across the matrix should be random. At the same time, the number of 1's should be fixed. Something like this {1 0 1 0 1 0; 0 1 0 1 0 0; 1 0 1 0 1 0; 0 1 0 1 0 1; 0 0 0 0 0 1 0 0 0 0 0 0} I tried using randperm randi .but I am not getting equal number of 1's Thanks in advance
0 个评论
采纳的回答
Azzi Abdelmalek
2016-8-15
编辑:Azzi Abdelmalek
2016-8-15
n=6
A=zeros(n)
m=2 % number of 1 per column
for k=1:n
idx=randperm(n,m)
A(idx,k)=1
end
更多回答(2 个)
José-Luis
2016-8-15
numCol = 5;
numOnes = 3;
numRows = 10;
result = ones(numRows,numCol);
for ii = 1:numCol
result(:,ii) = randperm(numRows) <= numOnes;
end
Azzi Abdelmalek
2016-8-15
n=6
m=2
A=zeros(n)
A(cell2mat(arrayfun(@(x) randperm(n,m)+6*x,0:n-1,'un',0)))=1
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Random Number Generation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!