How to produce a binary matrix from the original matrix

4 vues (au cours des 30 derniers jours)
Zhan
Zhan le 9 Avr 2017
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 ....

Réponses (1)

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

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by