Robochameleon  v1.0
kmeans_v1.m
Go to the documentation of this file.
1 
2 function centers = kmeans_v1(initPosArray, symbVector, varargin)
3 
4 % we want row vectors:
5 if size(initPosArray,2) == 1
6  initPosArray = initPosArray.';
7 end
8 
9 if size(symbVector,2) == 1
10  symbVector = symbVector.';
11 end
12 
13 % default values:
14 iterations = 10; % Max. number of iterations
15 tol = 2e-3; % Convergence tolerance
16 gamma = 0.5; % Adaptation step
17 
18 % configure optional variables:
19 if nargin > 2
20  for argidx = 1:2:nargin-2
21  switch varargin{argidx}
22  case 'gamma'
23  gamma = varargin{argidx+1};
24  case 'iterations'
25  iterations = varargin{argidx+1};
26  case 'tol'
27  tol = varargin{argidx+1};
28  end
29  end
30 end
31 
32 K_Centers = initPosArray;
33 deltaK = initPosArray;
34 
35 d = nan(length(K_Centers), length(symbVector)); % Alocate matrix of data-centers distance.
36 
37 %% k-means
38 for n = 1:iterations
39 
40  for ii = 1:length(K_Centers) % Calculate distances from all datapoints to each center
41  d(ii,:) = abs(K_Centers(ii)-symbVector);
42  end
43  [~, ind] = min(d);
44 
45  for ii = 1:length(K_Centers)
46  deltaK(ii) = gamma*(K_Centers(ii)-mean(symbVector(ind == ii))); % Incremental position values of each center
47  K_Centers(ii) = K_Centers(ii) - deltaK(ii); % Update center positions
48  end
49 
50  if sum(abs(deltaK))/length(K_Centers) < tol % Convergence test (if average position increment is less than 2e-3, stop iterations)
51  break;
52  end
53 
54 end
55 centers = K_Centers;
56 end
function kmeans_v1(in initPosArray, in symbVector, in varargin)
This function applies the k-means algorithm to find the centers of complex-valued data clusters...