How to produce a binary matrix from the original matrix

3 visualizaciones (últimos 30 días)
Zhan
Zhan el 9 de Abr. de 2017
Respondida: Andrei Bobrov el 10 de Abr. de 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 ....

Respuestas (1)

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

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by