根据Mathematica中的另一个列表值拆分列表

根据Mathematica中的另一个列表值拆分列表

问题描述:

在Mathematica中,我有一个点坐标列表

In Mathematica I have a list of point coordinates

size = 50;
points = Table[{RandomInteger[{0, size}], RandomInteger[{0, size}]}, {i, 1, n}];

以及这些点所属的聚类索引列表

and a list of cluster indices these points belong to

clusterIndices = {1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1};

基于clusterIndices值将点分为两个单独的列表的最简单方法是什么?

what is the easiest way to split the points into two separate lists based on the clusterIndices values?

我想出的解决方案:

pointIndices =
  Map[#[[2]] &,
    GatherBy[MapIndexed[{#1, #2[[1]]} &, clusterIndices], First],
    {2}];
pointsByCluster = Map[Part[points, #] &, pointIndices];

有更好的方法吗?

如何?

points[[
    Flatten[Position[clusterIndices, #]]
    ]] & /@
 Union[clusterIndices]