counting the number of clusters
Show older comments
I have a list of pair of numbers for example (please see below). In this example, if we look at the first column, the number one (1) repeats 3 times with its pair 54, 106 and 143. Similarly the number 24 repeats two times with its corresponding pair 87 and 288. What i want to do is group all those that repeat to one cluster. In the example listed below there are 12 pairs. I want to group 1 with 54, 106, and 143 and call it as one cluster and do the same thing with any such repeating pairs (number 24 in this example). In the end I will have 12 - 2 = 10 clusters. I would appreciate if some one could help with a matlab code for this.
Thanks,
Sudharsan
[1 54
1 106
1 143
5 90
24 87
64 244
5 202
7 270
24 288
25 176
26 206
27 161]
4 Comments
Stephan
on 8 Oct 2019
maybe i did not unterstand- I see only 8 clusters here - where is my mistake?
Sudharsan Srinivasan
on 8 Oct 2019
Sudharsan Srinivasan
on 9 Oct 2019
Accepted Answer
More Answers (1)
Image Analyst
on 8 Oct 2019
I agree with Stephan and findgroups() -- there are 8 "clusters."
Below I use findgroups() to find the groups, then I store all the rows (actually the values in the second column) into a cell array, where each cell has the second values for that group. Try this:
m = [
1 54
1 106
1 143
5 90
24 87
64 244
5 202
7 270
24 288
25 176
26 206
27 161]
groupIndexes = findgroups(m(:, 1))
% Make clusters as a cell array because every group might have a different number of members.
for k = 1 : max(groupIndexes)
thisGroupRows = groupIndexes == k;
groupValues{k} = m(thisGroupRows, 2);
end
celldisp(groupValues) % Report to the command window
You'll see this:
groupIndexes =
1
1
1
2
4
8
2
3
4
5
6
7
groupValues{1} =
54
106
143
groupValues{2} =
90
202
groupValues{3} =
270
groupValues{4} =
87
288
groupValues{5} =
176
groupValues{6} =
206
groupValues{7} =
161
groupValues{8} =
244
Is that what you want? If you want, you could put those values into the second column of the cell array and have the group value (the column 1 values) in the first column.
9 Comments
Sudharsan Srinivasan
on 9 Oct 2019
Sudharsan Srinivasan
on 10 Oct 2019
Fabio Freschi
on 10 Oct 2019
Good to know the availability of this function: +1
Sudharsan Srinivasan
on 10 Oct 2019
Image Analyst
on 10 Oct 2019
Since every number always "touches" three to five other numbers, why do you not ALWAYS have one cluster? Please give a label matrix where you assign the cluster number to the position so we can try to figure out what constitutes a group.
Perhaps you need bwlabel() or bwconncomp() but I'm not sure because I don't understand your algorithm for defining clusters.
Sudharsan Srinivasan
on 10 Oct 2019
Image Analyst
on 10 Oct 2019
Do you mean like this:
>> groupIDs = (m<=2)+1
groupIDs =
2 2
2 1
2 1
2 1
2 1
2 1
The top 2 touches the 1's and they have a path going down to the other 2's, so it seems like you want everything that's 2 or less in one group, and above 2 in another group.
Sudharsan Srinivasan
on 10 Oct 2019
Image Analyst
on 12 Oct 2019
Not totally, but try using this:
inBothColumns = m(:, 1) == m(:, 2);
This will give you a logical vector where both numbers in a row are the same.
Categories
Find more on Big Data Processing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!