counting the number of clusters

조회 수: 7 (최근 30일)
Sudharsan Srinivasan
Sudharsan Srinivasan 2019년 10월 8일
댓글: Image Analyst 2019년 10월 12일
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
Stephan
Stephan 2019년 10월 8일
편집: Stephan 2019년 10월 8일
Sorry, I still dont get it... since the 1 appears 3 times, the 5 and the 24 appear 2 times both - for me this are 3 clusters builded by 7 pairs. 5 pairs remain, which gives 8 clusters for me...
Sudharsan Srinivasan
Sudharsan Srinivasan 2019년 10월 9일
Hi Stephan, I think interpreted counting the clusters in wrong way. I think you're right. 3 clusters + 5 pairs = 8.
Thanks, Sudharsan

댓글을 달려면 로그인하십시오.

채택된 답변

Fabio Freschi
Fabio Freschi 2019년 10월 8일
편집: Fabio Freschi 2019년 10월 8일
% unique indices
idx = unique(data(:,1));
% clusters
cluster = arrayfun(@(idx)data(data(:,1) == idx,2),idx,'UniformOutput',false);
Then you can access your clusters with idx(i)and cluster{i}, where i = 1:length(idx)
  댓글 수: 3
Fabio Freschi
Fabio Freschi 2019년 10월 8일
편집: Fabio Freschi 2019년 10월 8일
do you mean the 2 here?
cluster = arrayfun(@(idx)data(data(:,1) == idx,2),idx,'UniformOutput',false);
% ^
% |
I am taking the second column of data to check the values of the cluster.
arrayfun is not simple to understand for newbies, try with
doc arrayfun
Roughly speaking, it basically applys the comparison data(:,1) == idx of the cluster to each value of idx, one element at a time. Then I extract the values of the second column of data using the previous comparison data(data(:,1) == idx,2). 'UniformOutput',false is needed to deal with the output and put every value in a different entry of the cell array cluster
Sudharsan Srinivasan
Sudharsan Srinivasan 2019년 10월 8일
Hi Fabio, Yes i meant the 2 where you have pointed it out. Now it makes sense to me. Thanks for your time. Sudharsan

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2019년 10월 8일
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
Sudharsan Srinivasan
Sudharsan Srinivasan 2019년 10월 10일
Actually not. Say, you imagine all the numbers in the matrix m as circles. So there are 7 circles where circle 1 touches 2,3,4 and also circle 2 touches 5,6,7, and in the end since all are conntected they form one cluster. I want to check if any number in column two appears again in column one (which in the example above is number '2') then i group them together ans call it a cluster. May be if that is clear?
Image Analyst
Image Analyst 2019년 10월 12일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by