3

男性3人と女性2人のペアのセットのすべての組み合わせを生成したい。ペアリングの例はたくさんありますが (たとえば、こちらを参照)、ペアのセットを扱っているものはありません。

たとえば、私が持っている場合:

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には、セット内の男性と女性のインデックスが含まれます (最初の列は男性、2 番目の列は女性、3 番目の列は男性、4 番目の列は女性など)。

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 に答える