【问题标题】:Combinations with multiple sets in c#c#中与多个集合的组合
【发布时间】:2018-05-30 15:55:50
【问题描述】:

我有三组积分。我需要所有组合每组最多有一个点。

从上图中的结果应该是:

  • p1、p3、p6
  • p1、p3、p7
  • p1、p4、p6
  • p1、p4、p7
  • p1、p5、p6
  • p1、p5、p7
  • p2、p3、p6
  • p2、p3、p7
  • p2、p4、p6
  • p2、p4、p7
  • p2、p5、p6
  • p2、p5、p7
  • p1,p3
  • p1, p4
  • p1,p5
  • ...
  • ...
  • p4,p6
  • ...
  • p1
  • p2
  • ...

我在

中使用Combinations的方法

Accord.Math

在所有集合中的所有点上,然后我从同一集合中删除包含更多点的组合。 我不想计算组合,而是想计算从 Set1 到 Set3 的所有可能路径(也传递给 Set2),因为组合对于一大组点来说花费了太多时间。 如何更改我的代码?

  var result1 = new Dictionary<int, List<List<ClusterInPath>>>();      
  foreach (KeyValuePair<int, List<ClusterInPath>> currentClustersInImage in allClustersInVertebra)
  {
    var allPaths = currentClustersInImage.Value.ToArray().Combinations().Select(el => el.ToList()).ToList();
    var filterPaths = new List<List<ClusterInPath>>();
    foreach (List<ClusterInPath> path in allPaths)
    {
      if(PathContainsMoreClustersOnSameImage(path)) continue;
       filterPaths.Add(path);
    }       
    result1.Add(currentClustersInVertebra.Key, filterPaths);
  }

类 ClusterInPath 定义如下:

  public class ClusterInPath
  {
    public ClusterInPath(int imageIndex, int clusterIndex)
    {
      ImageIndex = imageIndex;
      ClusterIndex = clusterIndex;
    }

    public int ImageIndex { get; set; }
    public int ClusterIndex { get; set; }
   }

提前致谢

【问题讨论】:

    标签: c# performance combinations accord.net


    【解决方案1】:

    将虚构(零、空等)元素添加到每个集合中,并获得集合的笛卡尔积。在输出过程中忽略或删除这些元素。

    Example of computing Cartesian product with LINQ

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 2011-06-21
      • 2013-01-26
      • 2012-11-27
      • 2017-11-28
      相关资源
      最近更新 更多