3

我想生成 3 男 2 女的所有组合。配对的例子有很多(例如参见this),但没有一个涉及配对集。

例如,如果我有:

Men   = {'M1', 'M2'};
Women = {'W1', 'W2', 'W3'};

我想要的结果是以下几组:

(M1, W1), (M2, W2)
(M1, W1), (M2, W3)
(M1, W2), (M2, W1)
(M1, W2), (M2, W3)
(M1, W3), (M2, W1)
(M1, W3), (M2, W2)

谢谢你。

4

3 回答 3

2

其实很简单。要填充一组k对,您需要k男和k女,所以让我们首先找到k男和k女的所有可能组合:

%// Find all possible combinations of sets of k pairs of men and women
k = 2;
idx_m = nchoosek(1:numel(Men), k);             % // Indices of men
idx_w = nchoosek(1:numel(Women), k);           % // Indices of women
idx_w = reshape(idx_w(:, perms(1:k)), [], k);  % // All permutations

然后让我们构造所有可能的k男人和k女人集合的组合:

[idx_comb_w, idx_comb_m] = find(ones(size(idx_w , 1), size(idx_m , 1)));
idx = sortrows([idx_m(idx_comb_m(:), :), idx_w(idx_comb_w(:), :)]);
idx = idx(:, reshape(1:size(idx, 2), k, [])'); %'// Rearrange in pairs

生成的矩阵idx包含集合中男性和女性的索引(第一列为男性,第二列为女性,第三列为男性,第四列为女性,依此类推……)。

例子

Men = {'M1', 'M2'};
Women = {'W1', 'W2', 'W3'};

%// Find all possible combinations of sets of k pairs of men and women
k = 2;
idx_m = nchoosek(1:numel(Men), k);
idx_w = nchoosek(1:numel(Women), k);
idx_w = reshape(idx_w(:, perms(1:k)), [], k);
[idx_comb_w, idx_comb_m] = find(ones(size(idx_w , 1), size(idx_m , 1)));

%// Construct pairs from indices and print sets nicely
idx = sortrows([idx_m(idx_comb_m(:), :), idx_w(idx_comb_w(:), :)]);
idx = idx(:, reshape(1:size(idx, 2), k, [])');

%// Obtain actual sets
sets = cell(size(idx));
sets(:, 1:2:end) = Men(idx(:, 1:2:end));
sets(:, 2:2:end) = Women(idx(:, 2:2:end));

%// Print sets nicely
sets_t = sets';
fprintf([repmat('(%s, %s), ', 1, k - 1), '(%s, %s)\n'], sets_t{:})

这里生成的数组sets已经适应了包含 和 的实际MenWomen。结果是:

(M1, W1), (M2, W2)
(M1, W1), (M2, W3)
(M1, W2), (M2, W1)
(M1, W2), (M2, W3)
(M1, W3), (M2, W1)
(M1, W3), (M2, W2)
于 2013-08-06T16:21:23.617 回答
0

此示例使用FEX 文件 allcomb

men = {'M1', 'M2', 'M3'};
women = {'W1', 'W2'};
allPeople = [men, women];
%// Play with vector index because allcomb doesn't work with cell
[~, id_men] = ismember(men,allPeople);
[~, id_women] = ismember(women,allPeople);


%// give all combinations for men/women
setOfMenWomen = allcomb(id_men,id_women);
%// give all combinations of pairs
nComb = size(setOfMenWomen,1);
setOfPair = nchoosek(1:nComb,2);
%// give all combinations for men/women/men/women
setOfPairMenWomen = cell2mat(arrayfun(@(id) setOfMenWomen(id,:), setOfPair, 'UniformOutput', 0));
%// get ids of set of pairs with the same men twice
isDoubleMen = setOfPairMenWomen(:,1) == setOfPairMenWomen(:,3);
%// get ids of set of pairs with the same women twice
isDoubleWomen = setOfPairMenWomen(:,2) == setOfPairMenWomen(:,4);
%// We don't want to have set of pairs with twice same men or same women
cond = isDoubleWomen | isDoubleMen;
setOfPairMenWomen(cond,:) = [];

%//results :
allPeople(setOfPairMenWomen)
于 2013-07-28T17:25:37.680 回答
0

我看不出你的问题到底是什么。您是否在 MATLAB 中搜索算法或实现?我将尝试用数学方法来回答。

您也许可以通过将其表述为图形问题来解决您的问题:您的一组人是一组顶点。通过将所有男人与所有女人联系起来形成边缘。现在找到(最大)匹配的集合。

这有点取决于您到底想要什么:例如,您是否希望元素 ((M1, W1)) 成为您解决方案的一部分?然后您正在搜索匹配项(有关解决方案集中元素的数量,请参阅Hosoya 索引)。如果您只想要无法添加进一步配对的元素,那么您只考虑最大匹配。

如果您将问题扩展到通用的非性别对,这可能会有所帮助:http://en.wikipedia.org/wiki/Telephone_number_(mathematics)

于 2013-09-11T14:55:31.387 回答