フィルターのクリア

Creating binary matrix with at least q ones on each row

2 ビュー (過去 30 日間)
Josue Sandoval
Josue Sandoval 2016 年 10 月 23 日
コメント済み: Josue Sandoval 2016 年 10 月 24 日
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
  1 件のコメント
Josue Sandoval
Josue Sandoval 2016 年 10 月 24 日
Very good answer, thanks!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFeature Detection and Extraction についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by