All possible combinations (subgroups vary in size) of elements in a vector

Hey,
I am trying to find a solution for the following problem:
I want to generate every possible combination of elements in a vector. The vector elements should always be split up in 2 groups. The groups can vary in size (number of elements), but all elements have to been included in the groups. The number of elements in a vector varies from 3 to 12.
Vector1 = [1 2 3 4]
Possible subgroups:
1 vs. 2,3,4
2 vs. 1,3,4
3 vs. 1,2,4
4 vs. 1,2,3
1,2, vs. 3,4
1,3 vs. 2,4
1,4 vs. 2,3
Vector2 =[1 2 3 4 5 6]
Size Subgroup1 = 1, Subgroup2 = 5
1 vs. 2,3,4,5,6
2 vs. 1,3,4,5,6
6 vs 1,2,3,4,5
Size Subgroup1 = 2, Subgroup2 = 4
1,2 vs. 3,4,5,6
1,3 vs. 2,4,5
Size Subgroup1 = 3, Subgroup2 = 3
1,2,3 vs 4,5,6
1,2,4 vs 3,5,6
Size Subgroup1 = 4, Subgroup2 = 2
Size Subgroup1 = 5, Subgroup2 = 1
I have searched for a solution but I haven´t found one yet. Thanks!
Stephan

1 个评论

Hi, I'm searching for the solution of this problem too.
Do you find anything useful in this months ?
Thanks.
Andrea

请先登录,再进行评论。

 采纳的回答

This might be what you are looking for:
>> C = partitions(4);
>> partdisp(C)
The 15 partitions of set {1 2 3 4}:
{1 2 3 4}
{1 2 3} {4}
{1 2 4} {3}
{1 2} {3 4}
{1 2} {3} {4}
{1 3 4} {2}
{1 3} {2 4}
{1 3} {2} {4}
{1 4} {2 3}
{1} {2 3 4}
{1} {2 3} {4}
{1 4} {2} {3}
{1} {2 4} {3}
{1} {2} {3 4}
{1} {2} {3} {4}

更多回答(1 个)

Thanks for your responses. I found a solution, but it is too embarassing to post it since there is also a very easy way to solve the problem. thanks again!

类别

帮助中心File Exchange 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by