Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

# here I am trying to get the random matrix with the elements only 1 and 0 where none of columns and rows can be 0 only and also the number of 0s and 1s should be in a percentage of 30% and 70%.

조회 수: 1 (최근 30일)
gkboy 2018년 8월 7일
마감: MATLAB Answer Bot 2021년 8월 20일
I am trying to get the random matrix with the elements only 1 and 0 where none of columns and rows can be 0s only or 1s only, and also the number of 0s and 1s should be in a percentage of 30% and 70%. They have to use probability
Requirements
1. Random Matrix
2. elements only 1s and 0s
3. percentage of 0s: 1s is 3:7
4. None of the columns and rows can be with only 0 and only 1. have to have mix
##### 댓글 수: 2없음 표시없음 숨기기
Walter Roberson 2018년 8월 8일
When you say that you need to use probability, then do you mean that the fraction should be defined as exactly 30/100, that instead each entry should have a 0.3 probability of being 0?
"None of the columns and rows can be with only 0 and only 1. have to have mix"
That is impossible to achieve if there is only one row or only one column.
Any time you enforce that any one row or column cannot be all the same, you force yourself away from strict uniform distribution.
Image Analyst 2018년 8월 8일

### 답변 (2개)

Thorsten 2018년 8월 8일
편집: Thorsten 2018년 8월 8일
N=5; % size
ok = false;
maxiter = 1000;
i = 1;
while ~ok & i < maxiter
z=zeros(N); % start w/ none
o=randperm(numel(z),round(0.7*numel(z))); % 70% are 1's
z(o)=1;
ok = ~any(all(z) | all(z, 2)' | all(~z) | all(~z, 2)');
i = i + 1;
end
##### 댓글 수: 0이전 댓글 -2개 표시이전 댓글 -2개 숨기기

dpb 2018년 8월 8일
N=5; % size
z=zeros(N); % start w/ none
o=randperm(numel(z),fix(numel(z)/2)); % 30% < ~50% < 70%
z(o)=1; % set the ones
>> ok=~(any(all(z) & all(z,2).')) % did we meet criteria?
ok =
logical
1
>>
Ayup...
##### 댓글 수: 2없음 표시없음 숨기기
Thorsten 2018년 8월 8일
편집: Thorsten 2018년 8월 8일
ok is true if there are only 1's in row A but not only ones in the corresponding column A. But this is not ok. Also the OP asks for no columns/rows with all zeros.
dpb 2018년 8월 8일
Yeah, the logic for the ok test isn't complete; the particular case was ok by inspection; it likely isn't a realistic ploy for practice; just a throwaway and probably shouldn't'a bothered...

이 질문은 마감되었습니다.

### Community Treasure Hunt

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

Start Hunting!

Translated by