Posts Tagged "clusters"

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 bwlabel(A,8) instead of bwlabel(A,4) Here is the script: A = [ 1 1 1 ; 0 0 1 ; 1 1 0]; [LabeledClusters NumberOfClusters] = bwlabel(A,4); s = regionprops(LabeledClusterMatrix,'Area'); AreaOfEachCluster =...

Read More