Home > atmlab > circular > circ_clust.m

circ_clust

PURPOSE ^

SYNOPSIS ^

function [cid, alpha, mu] = circ_clust(alpha, numclust, disp)

DESCRIPTION ^

 [cid, alpha, mu] = circClust(alpha, numclust, disp)
   Performs a simple agglomerative clustering of angular data.

   Input:
     alpha     sample of angles
     numclust  number of clusters desired, default: 2
     disp      show plot at each step, default: false

   Output:
     cid       cluster id for each entry of alpha
     alpha     sorted angles, matched with cid
     mu        mean direction of angles in each cluster

   Run without any input arguments for demo mode.

 Circular Statistics Toolbox for Matlab

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

DOWNLOAD ^

circ_clust.m

SOURCE CODE ^

0001 function [cid, alpha, mu] = circ_clust(alpha, numclust, disp)
0002 %
0003 % [cid, alpha, mu] = circClust(alpha, numclust, disp)
0004 %   Performs a simple agglomerative clustering of angular data.
0005 %
0006 %   Input:
0007 %     alpha     sample of angles
0008 %     numclust  number of clusters desired, default: 2
0009 %     disp      show plot at each step, default: false
0010 %
0011 %   Output:
0012 %     cid       cluster id for each entry of alpha
0013 %     alpha     sorted angles, matched with cid
0014 %     mu        mean direction of angles in each cluster
0015 %
0016 %   Run without any input arguments for demo mode.
0017 %
0018 % Circular Statistics Toolbox for Matlab
0019 
0020 % By Marc J. Velasco and Philipp Berens, 2009
0021 % velasco@ccs.fau.edu
0022 % Distributed under Open Source BSD License
0023 
0024 
0025 if nargin < 2, numclust = 5; end;
0026 if nargin < 3, disp = 0; end
0027 if nargin < 1
0028     % demo mode
0029     n = 20;
0030     alpha = 2*pi*rand(n,1)-pi;
0031     numclust = 4;
0032     disp = 1;
0033 end;
0034 
0035 n = length(alpha);
0036 if n < numclust, error('Not enough data for clusters.'), end
0037 
0038 % prepare data
0039 cid = (1:n)';
0040 
0041 % main clustering loop
0042 num_unique = length(unique(cid));
0043 num_clusters_wanted = numclust;
0044 
0045 while(num_unique > num_clusters_wanted)
0046 
0047     % find centroid means...
0048     
0049     % calculate the means for each putative cluster
0050     mu = NaN(n,1);
0051     for j=1:n
0052         if sum(cid==j)>0
0053             mu(j) = circ_mean(alpha(cid==j)');
0054         end
0055     end
0056     
0057     % find distance between centroids...
0058     mudist = abs(circ_dist2(mu));
0059 
0060     % find closest pair of clusters/datapoints
0061     mindist = min(mudist(tril(ones(size(mudist)),-1)==1));
0062     [row, col] = find(mudist==mindist);
0063     
0064     % update cluster id's
0065     cid(cid==max(row)) = min(col);
0066 
0067     % update stop criteria
0068     num_unique = length(unique(cid));
0069 
0070 end
0071 
0072 % renumber cluster ids (so cids [1 3 7 10] => [1 2 3 4]
0073 cid2 = cid;
0074 uniquecids = unique(cid);
0075 for j=1:length(uniquecids)
0076    cid(cid2==uniquecids(j)) = j;
0077 end
0078 
0079 % compute final cluster means
0080 mu = NaN(num_unique,1);
0081 r = NaN(num_unique,1);
0082 for j=1:num_unique
0083     if sum(cid==j)>0
0084         mu(j) = circ_mean(alpha(cid==j)');
0085         r(j) = circ_r(alpha(cid==j)');
0086     end
0087 end
0088 
0089 if disp
0090     % plot output
0091     z2 = exp(i*alpha);
0092     plotColor(real(z2), imag(z2), cid, 2)
0093     zmu = r.*exp(i*mu);
0094     plotColor(real(zmu), imag(zmu), 1:num_unique, 2, '*', 10, 1)
0095 
0096     axis square
0097     set(gca, 'XLim', [-1, 1]);
0098     set(gca, 'YLim', [-1, 1]);
0099 end
0100 
0101 
0102 function plotColor(x, y, c, varargin)
0103 % FUNCTION plotColor(x, y, c, [figurenum], [pstring], [markersize], [overlay_tf]);
0104 % plots a scatter plot for x, y, using color values in c (c should be
0105 % categorical info), with c same size as x and y;
0106 % fourth argument can be figure#, otherwise, uses figure(1);
0107 %
0108 % colors should be positive integes
0109 
0110 % copyright (c) 2009 Marc J. Velasco
0111 
0112 if nargin < 4
0113     figurenum = 1;
0114 else
0115    figurenum = varargin{1}; 
0116 end
0117 if nargin < 5
0118     pstring = '.';
0119 else
0120    pstring = varargin{2}; 
0121 end
0122 if nargin < 6
0123     ms = 10;
0124 else
0125    ms = varargin{3}; 
0126 end
0127 if nargin < 7
0128     overlay = 0;
0129 else
0130    overlay = varargin{4}; 
0131 end
0132 
0133 csmall = unique(c);
0134 figure(figurenum);
0135 if ~overlay, close(figurenum); end
0136 figure(figurenum);
0137 
0138 colors={'y', 'b', 'r', 'g', 'c', 'k', 'm'};
0139 
0140 hold on;
0141 for j=1:length(csmall);
0142     
0143    ci = (c == csmall(j));
0144    plot(x(ci), y(ci), strcat(pstring, colors{mod(j, length(colors))+1}), 'MarkerSize', ms);
0145     
0146 end
0147 if ~overlay, hold off; end
0148 figure(figurenum)
0149 
0150 
0151

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005