How do I create a matrix with all binary combinations?
24 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Christian P
le 13 Mai 2020
Commenté : Pranay Agarwal
le 2 Déc 2022
Hi
I want to create a matrix with all binary combinations. If N is the length of the binary code, there would be
possible combinations. Below are
, and the 16 possible binary combinations are displayed.
1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0
1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0
1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
How can I create such a matrix automatically? I don't really care which order they are in, as long as every combination is present, and N is a variable.
Thanks in advance.
0 commentaires
Réponse acceptée
the cyclist
le 13 Mai 2020
There's an incredible obfuscated hack for this:
N = 4;
dec2bin(0:2^N-1)' - '0'
Plus de réponses (2)
James Tursa
le 13 Mai 2020
Modifié(e) : James Tursa
le 13 Mai 2020
dec2bin(0:2^N-1) - '0'
Note that this is only practical for relatively small values of N. Even moderatly large N can cause this to exceed your available memory.
5 commentaires
Voss
le 1 Déc 2022
N = 4; % length
base = 3;
output = dec2base(0:base^N-1,base)-'0';
disp(output);
Voir également
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!