I want to insert a groups of ones in between zeros. I want to display all the possibilities.

1 visualizzazione (ultimi 30 giorni)
For example: 8 zeros, 6 ones (two groups with 3 in each group). Insert ones as a group in all possible places between zeros. Then i want answer as 00001110000111, 01110001110000, 00111000111000, 00011100000111. But this is a small example. But i want for a general case.

Risposta accettata

Stephen23
Stephen23 il 21 Feb 2019
A not very efficient brute-force method:
>> C = repmat({0},1,8);
>> C(1:2) = {[1,1,1]};
>> D = num2cell(perms(1:8),2);
>> D = cellfun(@(x)[C{x}],D,'uni',0);
>> D = unique(vertcat(D{:}),'rows')
D =
0 0 0 0 0 0 1 1 1 1 1 1
0 0 0 0 0 1 1 1 0 1 1 1
0 0 0 0 0 1 1 1 1 1 1 0
0 0 0 0 1 1 1 0 0 1 1 1
0 0 0 0 1 1 1 0 1 1 1 0
0 0 0 0 1 1 1 1 1 1 0 0
0 0 0 1 1 1 0 0 0 1 1 1
0 0 0 1 1 1 0 0 1 1 1 0
0 0 0 1 1 1 0 1 1 1 0 0
0 0 0 1 1 1 1 1 1 0 0 0
0 0 1 1 1 0 0 0 0 1 1 1
0 0 1 1 1 0 0 0 1 1 1 0
0 0 1 1 1 0 0 1 1 1 0 0
0 0 1 1 1 0 1 1 1 0 0 0
0 0 1 1 1 1 1 1 0 0 0 0
0 1 1 1 0 0 0 0 0 1 1 1
0 1 1 1 0 0 0 0 1 1 1 0
0 1 1 1 0 0 0 1 1 1 0 0
0 1 1 1 0 0 1 1 1 0 0 0
0 1 1 1 0 1 1 1 0 0 0 0
0 1 1 1 1 1 1 0 0 0 0 0
1 1 1 0 0 0 0 0 0 1 1 1
1 1 1 0 0 0 0 0 1 1 1 0
1 1 1 0 0 0 0 1 1 1 0 0
1 1 1 0 0 0 1 1 1 0 0 0
1 1 1 0 0 1 1 1 0 0 0 0
1 1 1 0 1 1 1 0 0 0 0 0
1 1 1 1 1 1 0 0 0 0 0 0
  1 Commento
SOMBABU BEJJIPURAM
SOMBABU BEJJIPURAM il 27 Feb 2019
Consider a string of length 50. Consider 20-1's and 30-zeros. Need all the possibilities of 4 groups of 5 ones (should not be placed two groups of ones together) and zeros can be placed any way. How to display all the combinations?

Accedi per commentare.

Più risposte (1)

Jon Wieser
Jon Wieser il 21 Feb 2019
here's a start
unique(perms([zeros(1,8),111,111]),'rows');

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by