Creating binary matrix with at least q ones on each row

1 次查看(过去 30 天)
I have a matrix which is intended to represent gender, so I want it to be roughly 0's and 1's. I am doing it as follows:
sex=round(rand(m,n));
The problems is that sometimes generates rows with only 0's or 1's. I want to change it so that it always contains at least q 1's and at most k 0's. Is there an easy way to do this?

采纳的回答

Walter Roberson
Walter Roberson 2016-10-23
temp = [ones(m,q), zeros(m,k), round(rand(m,n-k-q))];
Now you can randomize the order within each row of temp
[~, idx] = sort( rand(m,n), 2);
sex = temp(sub2ind(size(temp), ndgrid(1:m,1:n), idx));
However: neither sex nor gender are binary. There are over a dozen intersex conditions; the wider field is Disorders of Sexual Development; and see http://www.joshuakennon.com/the-six-common-biological-sexes-in-humans/. There at least 19 catalogued genders; New York City recognizes 31 gender identities

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Random Number Generation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by