How to produce a binary matrix from the original matrix

4 ビュー (過去 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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by