# How to plot clusters with binary matrix and coordinates?

I have to lists, xcor and ycor that contain the $$x$$-axis and $$y$$-axis coordinates of $$N$$ nodes. The nodes are divided into $$C$$ non overlapping clusters. The clustering information is provided in a binary matrix $$Mmat$$ of size $$C\times N$$, where $$C$$ is the number of clusters. If $$M_{c,n}=1$$, then node $$n$$ belongs to cluster $$c$$.

How can I show the clusters graphically?

xcor = {0.0667, 0.1667, 0.1667, 0.1667, 0.1667, 0.1667, 0.5000, 0.5000, 0.5000, 0.3000, 0.5000, 0.5000, 0.8333, 0.8333, 0.8333, 0.8333, 0.8333, 0.8333, 1.1667, 1.0667, 1.1667, 1.1667, 1.1067, 1.1667, 1.3000, 1.5000, 1.4000, 1.5000, 1.6000, 1.5000, 1.7333, 1.8333, 1.5333, 1.8333, 1.7333, 1.6333};

ycor = {0.1667, 0.4000, 0.8333, 1.1067, 1.5000, 1.6333, 0.1667, 0.4000, 0.8333, 1.1667, 1.5000, 1.8333, 0.1667, 0.5000, 0.8333, 1.1667, 1.5000, 1.7333, 0.1667, 0.5000, 0.7333, 1.1667, 1.4000, 1.8333, 0.1667, 0.5000, 0.8333, 1.1667, 1.5000, 1.8333, 0.1667, 0.5000, 0.7333, 1.1467, 1.5000, 1.6333};

Mmat = {{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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Range[Length @ positions]]


lp2 = ListPlot[Extract[xy, List /@ positions]] /.
p_Point :> {Dynamic@EdgeForm[{Thin, CurrentValue["Color"]}],
Opacity[.2], Polygon@p[[1]]};

Show[lp, lp2]


lp3 = ListPlot[Extract[xy, List /@ positions]] /.
p_Point :> {Dynamic @ EdgeForm[{Thin, CurrentValue["Color"]}], Opacity[.2],
MeshPrimitives[ConvexHullMesh[Join @@
(MeshCoordinates[DiscretizeRegion@Circle[#, .05]] & /@  p[[1]])], 2]};

Show[lp, lp3]


• Thanks a lot. This is exactly what I need!
– MGK
May 4, 2021 at 10:47