Create a matrix of binary numbers generated by sequence
2 ビュー (過去 30 日間)
古いコメントを表示
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]
0 件のコメント
採用された回答
the cyclist
2014 年 3 月 30 日
output = dec2bin(2^n-1:-1:0)-'0'
9 件のコメント
Sodamn Insane
2019 年 3 月 27 日
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
2021 年 10 月 29 日
this - '0' was the whole trick. I wasted more than 2 hours trying to figure this out.
その他の回答 (2 件)
Azzi Abdelmalek
2014 年 3 月 30 日
編集済み: Azzi Abdelmalek
2014 年 3 月 30 日
n=3;
s=0:1;
idx=rem(nchoosek(0:2^n-1,n),2)+1;
out=flipud(unique(s(idx),'rows'))
görkem tatar
2021 年 6 月 18 日
y =dec2bin(x)
x = 'dec variable'
y = 'convertion of the dec variable to bin'
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!