Matlab for cluster analysis

Posted by on Jun 27, 2013 in Complexity Science | 0 comments

In this post I shall describe some useful Matlab functions for detecting and analysing clusters in a 2-D matrix.

If you have a matrix

e.g.  $latex A = \left( \begin{array}{ccc} 1 & 1 & 1 \\ 0 & 0 & 1 \\ 1 & 1 & 0 \end{array} \right) &s=-4$

and want to identify clusters, cluster size etc. then the following function will help:

A = [ 1 1 1 ; 0 0 1 ; 1 1 0]; % Generates a matrix
[LabeledClusters NumberOfClusters] = bwlabel(A,4); % Finds clusters

The first line will produce the matrix shown above. This matrix is then used as an argument in the bwlabel function. The additional input of the number 4 is to specify how a cluster is defined. If the input is 4 then an element in the array will only check its north, south, east and west neighbours in constructing a cluster. The alternative input is 8 in which case an element in the array will check north-east, north-west, south-east and south-west neighbours also.

The LabeledClusters matrix will be:

$latex A = \left( \begin{array}{ccc} 1 & 1 & 1 \\ 0 & 0 & 1 \\ 2 & 2 & 0 \end{array} \right) &s=-4$

and the NumberOfClusters will be a scalar. In this case NumberOfClusters is two as there are two clusters in the matrix A. To get a list (vector) of sizes of all the different clusters use the following:

s = regionprops(LabeledClusterMatrix,'Area');
AreaOfEachCluster = cat(1,s.Area);

The output AreaOfEachCluster is a list of the size of each cluster. The location in the list corresponds to the number of the cluster as given by the LabeledClusters matrix.

From here it is easy to obtain a histogram of cluster sizes (using Matlab’s hist() function), average cluster size etc. To experiment you should try using


instead of


Here is the script:

A = [ 1 1 1 ; 0 0 1 ; 1 1 0];
[LabeledClusters NumberOfClusters] = bwlabel(A,4);
s = regionprops(LabeledClusterMatrix,'Area');
AreaOfEachCluster = cat(1,s.Area);

About Kishan Manani

Kishan Manani is a PhD Student at the Center for Complexity Science and British Heart Foundation Centre of Research Excellence at Imperial College London. His research interests are in modelling cardiac dynamics, excitable media, atrial fibrillation and machine learning in medicine. The focus of his PhD is the development and study of simple models of cardiac dynamics to study the effects of tissue structure in atrial fibrillation. He also uses machine learning tools with a recently developed signals processing engine to data mine clinical electrograms, electrical signals from electrodes in contact with the atrial surface during catheter ablation therapy.

He obtained an MSci Physics degree at Imperial College London in 2012 where he was awarded the Callendar Prize for all round excellence in physics: his master’s thesis focused on developing cellular automata models and applying techniques from non-linear dynamics to investigate atrial fibrillation. Kishan actively engages in STEM outreach by giving talks and demonstrations in various London schools.

Kishan’s academic webpage