Clear Filters
Clear Filters

Find number of clusters remaining after doing agglomerative hierarchical clustering

2 views (last 30 days)
I used agglomerative hierarchical clustering and stopeed clutering by giving a cutoff value. now I need to get the number of clusters in that particular image in order to proceed. Can someone help me?

Answers (1)

Mann Baidi
Mann Baidi on 29 Apr 2024
I am assuming that you would like to get the number of clusters present in the data after using cutoff value in the agglomerative hierarchical clustering in MATLAB.
You can check the number of clusters in the data by using the code below for reference.
% Sample data
rng default;
data = randn(50, 2);
% Perform hierarchical clustering
Z = linkage(data, 'ward'); % 'ward' linkage method is used here, but other methods are available
idx = cluster(Z,'cutoff',0.4)
idx = 50x1
19 26 28 4 11 17 23 21 29 15
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
histogram(idx) % Plot histogram for the idx
figure;
% Plot data points colored by cluster
gscatter(data(:,1), data(:,2), idx);
hold on;
legend('off')
% Count the number of clusters
num_clusters = max(idx);
fprintf('Number of clusters: %d\n', num_clusters);
Number of clusters: 29
If you would like to know more about these functions we can go throught their documentation, using the following link.
Hoping this will help in resloving your query!

Community Treasure Hunt

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

Start Hunting!