根据Mathematica中的另一个列表值拆分列表 [英] Splitting a list based on another list values in Mathematica

查看:90
本文介绍了根据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]

这篇关于根据Mathematica中的另一个列表值拆分列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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