I want to create a binary matrix with a variable dimensions
이전 댓글 표시
I want to create a binary matrix of dimension k x M where k and M are user defined.(k is the number of rows and M is the number of column). And number of zeros and ones in each row are user defined. Also this binary matrix should be a function.
댓글 수: 3
James Tursa
2022년 8월 11일
What have you done so far? What specific problems are you having with your code?
Dyuman Joshi
2022년 8월 11일
Nir Vaishnav
2022년 8월 11일
답변 (1개)
David Hill
2022년 8월 11일
편집: David Hill
2022년 8월 11일
k=10;
M=50;
O=randi(M+1,k)-1;%number of ones per row (number of zeros per row= 50 - O)
for n=1:k
m=[ones(1,O(n)),zeros(1,M-O(n))];
Matrix(n,:)=m(randperm(M));
end
댓글 수: 2
Nir Vaishnav
2022년 8월 11일
David Hill
2022년 8월 11일
You just need to clear Matrix or set Matrix = [ ] at the beginning.
k=10;
M=50;
Matrix=[];
O=randi(M+1,k)-1;%number of ones per row (number of zeros per row= 50 - O)
for n=1:k
m=[ones(1,O(n)),zeros(1,M-O(n))];
Matrix(n,:)=m(randperm(M));
end
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!