问题
in matlab how do I do this? I have a set of n elements
from this set I make a new set of n/2 pairs such that elements in different pairs are distinct. how do I generate distinct sets of such n/2 pairs from n elements in matlab?
e.g. input set - {1,2,3,4}
possible output sets -
{{1,2},{3,4}}
{{1,3},{2,4}}
{{1,4},{2,3}}
回答1:
I could not find a clean solution for the "distinct elements for each halved-vector" requirement. Thus I suggest to check each result individually. I expect that there's a better solution around: this one just does its job.
x = [1 2 3 3];
xsize = size(x,2);
p = perms(x);
up = unique(p,'rows');
result = [];
for entry=up'
left = entry(1:xsize/2);
right = entry(xsize/2+1:xsize);
if numel(unique(left)) == xsize/2 && numel(unique(right)) == xsize/2
result = vertcat(result,entry')
end
end
Just for completeness, the result is:
1 3 2 3
1 3 3 2
2 3 1 3
2 3 3 1
3 1 2 3
3 1 3 2
3 2 1 3
3 2 3 1
I was not sure if you needed to actually split the halved vectors. In that case, just put left
and right
into whatever you prefer.
来源:https://stackoverflow.com/questions/10976347/how-to-generate-possible-distinct-sets-of-pairs-from-a-given-set