Create a matrix of binary numbers generated by sequence

10 vues (au cours des 30 derniers jours)
Mohammed
Mohammed le 30 Mar 2014
I want to create a general matrix to create outputs of the below format...
if n=1
output=[1 0]
if n=2
output=[1 1
1 0
0 1
0 0]
if n=3
output=[1 1 1
1 1 0
1 0 1
1 0 0
0 1 1
0 1 0
0 0 1
0 0 0]
for any number of n; the output matrix will be of the order of [2^n X n]

Réponse acceptée

the cyclist
the cyclist le 30 Mar 2014
output = dec2bin(2^n-1:-1:0)-'0'
  9 commentaires
Sodamn Insane
Sodamn Insane le 27 Mar 2019
The -'0' converts the output of dec2bin from a character array with each combination as a row element to a matrix with the type of double.
Muhammad Atif Ali
Muhammad Atif Ali le 29 Oct 2021
this - '0' was the whole trick. I wasted more than 2 hours trying to figure this out.

Connectez-vous pour commenter.

Plus de réponses (2)

Azzi Abdelmalek
Azzi Abdelmalek le 30 Mar 2014
Modifié(e) : Azzi Abdelmalek le 30 Mar 2014
n=3;
s=0:1;
idx=rem(nchoosek(0:2^n-1,n),2)+1;
out=flipud(unique(s(idx),'rows'))
  1 commentaire
Mohammed
Mohammed le 30 Mar 2014
Thank you so much.that was quick n short

Connectez-vous pour commenter.


görkem tatar
görkem tatar le 18 Juin 2021
y =dec2bin(x)
x = 'dec variable'
y = 'convertion of the dec variable to bin'

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!

Translated by