MATLAB Answers

# Creating all possible combination of 0 and 1 in 2D array

7 ビュー (過去 30 日間)
Luqman Saleem 2019 年 12 月 24 日

Let the size of 2D matrix is m (n rows, m cols).
How can I create all possible combinations of 0 and 1 in this matrix?

#### 0 件のコメント

サインイン to comment.

### 回答 (2 件)

the cyclist 2019 年 12 月 24 日
% Example choice of m and n
n = 2;
m = 3;
allCombos = reshape(dec2bin(0:2^(n*m)-1)-'0',n,m,[]);
allCombos is a 3-dimensional m*n*X array, where each slice in the 3rd dimension is one of the possible matrices.
Note that if m and n get large, you will quickly run out of memory. Combinatorics is a cruel mistress.

#### 1 件のコメント

Luqman Saleem 2019 年 12 月 25 日
Hey, thank you for your answer. It gives all combinations indeed. But I don't need repeating configration, for examples, in above example there are 6 configrations that are all zero. How can I get only unique configrations?

サインイン to comment.

the cyclist 2019 年 12 月 25 日
Did you mean you just want each possible unique row, rather than each possible unique matrix?
justRows = dec2bin(0:2^m-1)-'0'
where m is the number of columns.
If you mean something else, please give more info, because I can't think of any other interpretation of what you mean.

#### 0 件のコメント

サインイン to comment.

サインイン してこの質問に回答します。

Translated by