Matrix with binomial permutation

1 回表示 (過去 30 日間)
Zheng Liu
Zheng Liu 2017 年 2 月 13 日
コメント済み: Zheng Liu 2017 年 2 月 13 日
I really had trouble finding the right words for the question. I'll try my best. Given a numerical number k (k>1), I want to get a [2^k, k] matrix with all possible binomial permutation. for K = 2, I hope to get: [1 1; 1 2; 2 1; 2 2]. for k = 3, the matrix will look like: [1 1 1; 1 1 2; 1 2 1; 1 2 2; 2 1 1; 2 1 2; 2 2 1; 2 2 2]. I could do for loop for each condition, like for k=3:
mat = [];
for digit1 = 1:2
for digit2 = 1:2
for digit3 = 1:2
mat(size(mat,1)+1,:) = [digit1 digit2 digit3];
end
end
end
But this is not generalized and efficient. Is there any thoughts on generating such matrix? Thanks!

採用された回答

Honglei Chen
Honglei Chen 2017 年 2 月 13 日
dec2bin(0:2^K-1)-'0'+1
HTH
  1 件のコメント
Zheng Liu
Zheng Liu 2017 年 2 月 13 日
That's why we like binary. Thanks!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by