How to produce a binary matrix from the original matrix

1 view (last 30 days)
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 ....

Answers (1)

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

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!