如何从给定的集合中生成可能的不同对对? [英] how to generate possible distinct sets of pairs from a given set?

查看:78
本文介绍了如何从给定的集合中生成可能的不同对对?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在matlab中,我该怎么做? 我有一组n个元素

in matlab how do I do this? I have a set of n elements

从这个集合中我得到了一组新的n/2对,使得不同对中的元素是不同的. 如何从Matlab中的n个元素生成此类n/2对的不同集合?

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?

例如 输入集-{1,2,3,4}

e.g. input set - {1,2,3,4}

可能的输出集-

{{1,2,2,{3,4}}

{{1,2},{3,4}}

{{1,3},{2,4}}

{{1,3},{2,4}}

{{1,4},{2,3}}

{{1,4},{2,3}}

推荐答案

我找不到每个减半向量的不同元素"要求的干净解决方案.因此,我建议单独检查每个结果.我希望周围有一个更好的解决方案:这就是它的工作.

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

我不确定您是否需要实际拆分一半的向量.在这种情况下,只需将leftright放入您喜欢的任何内容中即可.

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.

这篇关于如何从给定的集合中生成可能的不同对对?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆