MATLAB: Permutation and combination to flip the numbers in the array

algorithm

There is a set of numbers such as [1 0 1 0 1 1], and now I need to flip two of them arbitrarily, such as flipping the first and the second, then the result is [0 1 1 0 1 1], I need to enumerate all flipped result.
The array (only 01) and the number of flips are variables. How to write a program?

Best Answer

A = logical([1 0 1 0 1 1])
nflip = 2;
m = size(A, 2);
j = nchoosek(1:m, nflip);
i = repmat((1:size(j,1))', [1,size(j,2)]);
mask = accumarray([i(:) j(:)], 1);
F = xor(A,mask)
Result
A =
1×6 logical array
1 0 1 0 1 1
F =
15×6 logical array
0 1 1 0 1 1
0 0 0 0 1 1
0 0 1 1 1 1
0 0 1 0 0 1
0 0 1 0 1 0
1 1 0 0 1 1
1 1 1 1 1 1
1 1 1 0 0 1
1 1 1 0 1 0
1 0 0 1 1 1
1 0 0 0 0 1
1 0 0 0 1 0
1 0 1 1 0 1
1 0 1 1 1 0
1 0 1 0 0 0