Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Actually it's quite simple. To populate a set of <em>k</em> pairs, you need <em>k</em> men and <em>k</em> women, so let's find all possible combinations of <em>k</em> men and <em>k</em> women first:</p> <pre><code>%// 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 </code></pre> <p>Then let's construct all possible combinations of sets of <em>k</em> men and <em>k</em> women:</p> <pre><code>[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 </code></pre> <p>The resulting matrix <code>idx</code> contains the indices of men and women in the sets (first column is men, second column - women, third column - men, and fourth column - women, and so on...).</p> <h3>Example</h3> <pre><code>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{:}) </code></pre> <p>Here the resulting array <code>sets</code> is already adapted to include the actual values from <code>Men</code> and <code>Women</code>. The result is:</p> <pre><code>(M1, W1), (M2, W2) (M1, W1), (M2, W3) (M1, W2), (M2, W1) (M1, W2), (M2, W3) (M1, W3), (M2, W1) (M1, W3), (M2, W2) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload