I want to create a binary matrix with a variable dimensions
Mostra commenti meno recenti
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 Commenti
James Tursa
il 11 Ago 2022
What have you done so far? What specific problems are you having with your code?
Dyuman Joshi
il 11 Ago 2022
Nir Vaishnav
il 11 Ago 2022
Risposte (1)
David Hill
il 11 Ago 2022
Modificato: David Hill
il 11 Ago 2022
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 Commenti
Nir Vaishnav
il 11 Ago 2022
David Hill
il 11 Ago 2022
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
Categorie
Scopri di più su Creating and Concatenating Matrices in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!