필터 지우기
필터 지우기

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Feature Detection and Extraction에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by