How do I create a matrix with all binary combinations?

104 visualizaciones (últimos 30 días)
Christian P
Christian P el 13 de Mayo de 2020
Comentada: Pranay Agarwal el 2 de Dic. de 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.

Respuesta aceptada

the cyclist
the cyclist el 13 de Mayo de 2020
There's an incredible obfuscated hack for this:
N = 4;
dec2bin(0:2^N-1)' - '0'
  1 comentario
Christian P
Christian P el 13 de Mayo de 2020
Thanks to both of you, this worked like a charm! My N is usually less than 10, so memory shouldn't be a big problem :)

Iniciar sesión para comentar.

Más respuestas (2)

Fangjun Jiang
Fangjun Jiang el 13 de Mayo de 2020
I though it should be
ff2n(4)
  2 comentarios
Christian P
Christian P el 13 de Mayo de 2020
This also worked, thanks :)
Fangjun Jiang
Fangjun Jiang el 14 de Mayo de 2020
ff2n, Two-level full-factorial design, is built for this.

Iniciar sesión para comentar.


James Tursa
James Tursa el 13 de Mayo de 2020
Editada: James Tursa el 13 de Mayo de 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 comentarios
Voss
Voss el 1 de Dic. de 2022
N = 4; % length
base = 3;
output = dec2base(0:base^N-1,base)-'0';
disp(output);
0 0 0 0 0 0 0 1 0 0 0 2 0 0 1 0 0 0 1 1 0 0 1 2 0 0 2 0 0 0 2 1 0 0 2 2 0 1 0 0 0 1 0 1 0 1 0 2 0 1 1 0 0 1 1 1 0 1 1 2 0 1 2 0 0 1 2 1 0 1 2 2 0 2 0 0 0 2 0 1 0 2 0 2 0 2 1 0 0 2 1 1 0 2 1 2 0 2 2 0 0 2 2 1 0 2 2 2 1 0 0 0 1 0 0 1 1 0 0 2 1 0 1 0 1 0 1 1 1 0 1 2 1 0 2 0 1 0 2 1 1 0 2 2 1 1 0 0 1 1 0 1 1 1 0 2 1 1 1 0 1 1 1 1 1 1 1 2 1 1 2 0 1 1 2 1 1 1 2 2 1 2 0 0 1 2 0 1 1 2 0 2 1 2 1 0 1 2 1 1 1 2 1 2 1 2 2 0 1 2 2 1 1 2 2 2 2 0 0 0 2 0 0 1 2 0 0 2 2 0 1 0 2 0 1 1 2 0 1 2 2 0 2 0 2 0 2 1 2 0 2 2 2 1 0 0 2 1 0 1 2 1 0 2 2 1 1 0 2 1 1 1 2 1 1 2 2 1 2 0 2 1 2 1 2 1 2 2 2 2 0 0 2 2 0 1 2 2 0 2 2 2 1 0 2 2 1 1 2 2 1 2 2 2 2 0 2 2 2 1 2 2 2 2
Pranay Agarwal
Pranay Agarwal el 2 de Dic. de 2022
Thanks a lot....

Iniciar sesión para comentar.

Etiquetas

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by