How can I obtain all possible combinations of a given vector

2 visualizaciones (últimos 30 días)
I have a vector A=[1 1 1 0 0 1 1]. I want all the possible combinations as following....
A=[1 1 1 0 0 0 0; 1 1 0 0 0 1 0; 1 1 0 0 0 0 1; 0 1 1 0 0 1 0; 0 1 1 0 0 0 1; 0 1 0 0 0 1 1]
  4 comentarios
Stephen23
Stephen23 el 2 de Oct. de 2020
Editada: Stephen23 el 2 de Oct. de 2020
Why is
0 1 0 0 0 1 1
included, but
0 0 1 0 0 1 1
1 0 0 0 0 1 1
are excluded?
Although you write in your question that you want "all combinations", it seems that you actually want a subset of the combinations, but so far the rules to derive that subset are not clear to me.
subhashree priyadarshini
subhashree priyadarshini el 2 de Oct. de 2020
Sir for my problem I need the second element of each row or column must be 1. Thats why I want to exclude the following rows...
0 0 1 0 0 1 1;
1 0 0 0 0 1 1;
1 0 1 0 0 0 1;
1 0 1 0 0 1 0;

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 2 de Oct. de 2020
Brute force, not particularly efficient:
>> A = [1,1,1,0,0,1,1];
>> P = unique(bsxfun(@and,A,unique(perms(A),'rows')),'rows');
>> P(sum(P,2)~=3,:)=[]
P =
0 0 1 0 0 1 1
0 1 0 0 0 1 1
0 1 1 0 0 0 1
0 1 1 0 0 1 0
1 0 0 0 0 1 1
1 0 1 0 0 0 1
1 0 1 0 0 1 0
1 1 0 0 0 0 1
1 1 0 0 0 1 0
1 1 1 0 0 0 0
  3 comentarios

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices 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