How to make a general code for a set of pairs between 2 men and 3 women

Hi, all
I just want to know how to make a code for a set of pairs between 2 men and 3 women. I used meshgrid function to generate pairs but I cannot make possible "a set of pairs". can you help me please? Thanks
An example of a set of pairs :
Given, Men = {M1, M2}; Women = {W1, W2, W3}
a set of pairs =
(M1, W1), (M2,W2)
(M1, W1), (M2,W3)
(M1, W2), (M2,W1)
(M1, W2), (M2,W3)
(M1, W3), (M2,W1)
(M1, W3), (M2,W2)

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2013년 7월 28일
편집: Andrei Bobrov 2013년 7월 29일
M = {'M1' 'M2'}
W = {'W1' 'W2' 'W3'}
w = perms(1:numel(W));
w = w(:,1:numel(M));
s = size(w);
out = mat2cell(reshape([M(ones(s(1),1)*(1:2));W(w)],s(1),[]),...
ones(s(1),1),s(2)*ones(1,s(2));
ADD
M = 100 + (1:4); % males
W = 50 + (1:3); % women
[s,ii] = sort(cellfun('length',{M,W}));
jj = perms(1:s(2));
p1 = d{ii(2)}(jj(:,1:s(1)));
p2 = ones(size(p1,1),1)*d{ii(1)};
out = reshape([p2;p1],[],s(1)*2);

댓글 수: 5

Thank you for your code. But I cannot see and check 'out' because it is a cell-type matrix. How can I see the out? Thanks.
Or can you make the out numbers if numbering Men as 101, 102 and Women as 1,2,3. Number would be better than cell to me. Thanks.
Jan
Jan 2013년 7월 28일
편집: Jan 2013년 7월 28일
@Jong: Why do you prefer number instead of cells? Why can't you see the contents of cells?
Just my preference :) BTW, when I increase number of women or men, your code produce wrong results. Can you check that with 4 Men and 3 Women?
ADD part in this answer

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Language Fundamentals에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by