MATLAB: All possible *UNIQUE* permutations of a binary vector

griddlernonogrampermspermutationsunique

hello all, i am trying to make a griddler/nonogram puzzel solver with matlab.
to do so i need to find all possible UNIQUE permutations of a binary vector. for example :
input Vector: [1 0 1 0]
output matrix:
[1 0 1 0
1 0 0 1
0 1 0 1
0 0 1 1
0 1 1 0
1 1 0 0]
untile now i've been using the following code:
if true
inVec=[1 0 1 0]
outMat=perms(inVec);
outMat=unique(outMat,'rows')
end
This was perfectly fine but for inVec longer then 10 i get an: 'out of memory' error.
is it possible to do this without using perms/unique ? i need this for up to inVec length 20.
Thanks Lampel.

Best Answer

c = [0 1];
cc = c(fullfact([2 2 2 2]));
out = cc(sum(cc,2) == 2,:);
ADD: use Roger Stafford's idea from answer
A = [1 1 0 0];
n = numel(A);
k = sum(A);
c = nchoosek(1:n,k);
m = size(c,1);
out = zeros(m,n);
out(sub2ind([m,n],(1:m)'*[1 1],c)) = 1;