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

조회 수: 20 (최근 30일)
Luqman Saleem
Luqman Saleem 2019년 12월 24일
댓글: the cyclist 2023년 1월 10일
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?

답변 (2개)

the cyclist
the cyclist 2019년 12월 24일
편집: the cyclist 2023년 1월 10일
% 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 n*m*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.

the cyclist
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.
  댓글 수: 2
Muhammad Naeem
Muhammad Naeem 2023년 1월 10일
Can I get each possible unique matrix there???
Thank You
the cyclist
the cyclist 2023년 1월 10일
Is the other answer I posted what you want? It gives each unique possible nxm matrix of 0's and 1's.
(I did a small fix in that code just now.)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Discrete Math에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by