Generating combination of binary numbers

2 ビュー (過去 30 日間)
Luqman Saleem
Luqman Saleem 2019 年 12 月 18 日
コメント済み: Luqman Saleem 2019 年 12 月 18 日
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
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...

採用された回答

Guillaume
Guillaume 2019 年 12 月 18 日
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
  1 件のコメント
Luqman Saleem
Luqman Saleem 2019 年 12 月 18 日
Wow! That was very quick. Slute!
I am so noob in programming.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by