実際、それは非常に簡単です。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
からの実際の値を含むように既に調整されています。結果は次のとおりです。Men
Women
(M1, W1), (M2, W2)
(M1, W1), (M2, W3)
(M1, W2), (M2, W1)
(M1, W2), (M2, W3)
(M1, W3), (M2, W1)
(M1, W3), (M2, W2)