Hi guys I have a matrix like this:
INPUT=[[1 0 2 2 0] [0 0 2 0 2] [2 2 2 2 2] [0 2 0 2 0] [0 0 3 0 0]]
I expect to see same cluster with their coordinates like this:
OUTPUT=[[(0, 0)], [(0, 2), (0, 3), (1, 2), (1, 4), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (3, 1), (3, 3)], [(4, 2)]]
.
[(0, 0)]
Refers 1's at first and its A one group.
[(0, 2), (0, 3), (1, 2), (1, 4), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (3, 1), (3, 3)]
Refers all 2's in matrix.
I wanted to list all of the clusters based on their coordinates as my example. Thank you.
CodePudding user response:
This should do the trick:
INPUT=[[1, 0, 2, 2, 0], [0, 0, 2, 0, 2], [2, 2, 2, 2, 2], [0, 2, 0, 2, 0], [0, 0, 3, 0, 0]]
clusters = {}
for i, row in enumerate(INPUT):
for j, x in enumerate(row):
if x in clusters:
clusters[x].append((i, j))
else:
clusters[x] = [(i, j)]
OUTPUT = []
maxi = max(clusters.keys())
for i in range(maxi):
OUTPUT.append(clusters.get(i, None))