Generating combination of binary numbers
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Luqman Saleem
le 18 Déc 2019
Commenté : Luqman Saleem
le 18 Déc 2019
Variable N and M are some integers and
. I want to create permutation of M 1s and
0s in the order shown below. Let
and
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/255415/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/255416/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/255417/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/255418/image.png)
A = [1 1 0 0 0;
1 0 1 0 0;
1 0 0 1 0;
1 0 0 0 1;
0 1 1 0 0;
0 1 0 1 0;
0 1 0 0 1;
0 0 1 1 0;
0 0 1 0 1;
0 0 0 1 1];
Note that in first row all 1s are on right side, then in next row the right most 1 move one step toward right and so on...
0 commentaires
Réponse acceptée
Guillaume
le 18 Déc 2019
nchoosek outputs the indices in your desired ordering, so it's easy:
N = 5, M = 2; %demo data
cols = nchoosek(1:N, M);
A = zeros(size(cols, 1), N);
A(sub2ind(size(A), repmat((1:size(A, 1))', 1, M), cols)) = 1
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!