How to produce a binary matrix from the original matrix

조회 수: 3 (최근 30일)
Zhan
Zhan 2017년 4월 9일
답변: Andrei Bobrov 2017년 4월 10일
Consider that there are 4 choices: A,B,C,D I want to find the possible combination among these four choices. For example, just choice 1, so the pattern will be (1,0,0,0). Or just choice 2 and 4, then pattern will be (0,1,0,1). Also, pattern (0,0,0,0) is possible (it means that non of the alternative choices). Total number of possible patterns is 16, like the following matrix:
P = [
0 0 0 0
1 0 0 0
0 1 0 0
1 1 0 0
0 0 0 1
0 1 0 1
1 0 0 1
0 1 1 0
0 0 1 0
1 1 0 0
1 0 1 0
1 1 1 0
0 0 1 1
0 1 1 1
1 0 1 1
1 1 1 1
];
% sequence of rows (possible pattern) in matrix P is not important.
I want to develop an algorithm which can find automatically these patterns. So that according to number of variables, matrix P can be changed.
e.g.: For 2 choices: possible patterns is 4 For 3 choices: possible patterns is 8 For 4 choices: possible patterns is 16 For 5 choices: possible patterns is 32 For 6 choices: possible patterns is 64 ....

답변 (1개)

Andrei Bobrov
Andrei Bobrov 2017년 4월 10일
[m,n] = size(C);
out = any(reshape(C,m,1,[])==(1:n),3)*2.^(m-1:-1:0)' + 1;

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by