All possible grouping combination for different package

Hi,
I want to group items in different packages and have all possible combinations. On the other hand, there must not be repetition with the same elements in a different order for the same package.
There are X elements that I would like to divide into Y groups with all possible combinations. For example, for X=5 elements: A, B, C, D, E and Y=2 groups, this should be as follow:
Group 1, Group 2
Combination 1: {A, B, C} , {D, E}
Combination 2: {A, B, D} , {C, E}
Combination 3: {A, D, E} , {B, C}
…
However, all grouping combination with the same element should be avoided, such as A,B,C or A,C,B.
Thanks

回答(1 个)

S = {'A', 'B', 'C', 'D', 'E'}
S = 1×5 cell array
{'A'} {'B'} {'C'} {'D'} {'E'}
Group1 = nchoosek(S, 3)
Group1 = 10×3 cell array
{'A'} {'B'} {'C'} {'A'} {'B'} {'D'} {'A'} {'B'} {'E'} {'A'} {'C'} {'D'} {'A'} {'C'} {'E'} {'A'} {'D'} {'E'} {'B'} {'C'} {'D'} {'B'} {'C'} {'E'} {'B'} {'D'} {'E'} {'C'} {'D'} {'E'}
and in each case, Group2 is setdiff(S) and the corresponding elements of group 1.

3 个评论

Hi,
Thanks for the answer. But I think it works only if Y groups = 2. I would like to have expandable methods.
The final idea is to select boxes (packaging) with different products. Each possible solution will be sent to an optimization function. I would like to evaluate the 2, 3, 4 combinations of boxes.
https://www.mathworks.com/matlabcentral/fileexchange/24133-set-partition gives code to partition into a fixed number of non-empty sets. In itself, it is not designed for fixed set sizes. Perhaps @Bruno Luong has an adaptation for that purpose.

请先登录,再进行评论。

类别

在 帮助中心 和 File Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by