MATLAB: How to obtain all possible combinations of a given vector

binomial coefficientmatrixpermutation

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]

Best Answer

  • 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