function [centers,clusters,errors,ind] = kmeans_clusters(sD, n_max, c_max, verbose)
% KMEANS_CLUSTERS Clustering with k-means with different values for k.
%
% [c, p, err, ind] = kmeans_clusters(sD, [n_max], [c_max], [verbose])
%
% [c, p, err, ind] = kmeans_clusters(sD);
%
% Input and output arguments ([]'s are optional):
% D (struct) map or data struct
% (matrix) size dlen x dim, the data
% [n_max] (scalar) maximum number of clusters, default is sqrt(dlen)
% [c_max] (scalar) maximum number of k-means runs, default is 5
% [verbose] (scalar) verbose level, 0 by default
%
% c (cell array) c{i} contains cluster centroids for k=i
% p (cell array) p{i} contains cluster indeces for k=i
% err (vector) squared sum of errors for each value of k
% ind (vector) Davies-Bouldin index value for each clustering
%
% Makes a k-means to the given data set with different values of
% k. The k-means is run multiple times for each k, and the best of
% these is selected based on sum of squared errors. Finally, the
% Davies-Bouldin index is calculated for each clustering.
%
% For example to cluster a SOM:
% [c, p, err, ind] = kmeans_clusters(sM); % find clusterings
% [dummy,i] = min(ind); % select the one with smallest index
% som_show(sM,'color',{p{i},sprintf('%d clusters',i)}); % visualize
% colormap(jet(i)), som_recolorbar % change colormap
%
% See also SOM_KMEANS.
% References:
% Jain, A.K., Dubes, R.C., "Algorithms for Clustering Data",
% Prentice Hall, 1988, pp. 96-101.
%
% Davies, D.L., Bouldin, D.W., "A Cluster Separation Measure",
% IEEE Transactions on Pattern Analysis and Machine Intelligence,
% vol. PAMI-1, no. 2, 1979, pp. 224-227.
%
% Vesanto, J., Alhoniemi, E., "Clustering of the Self-Organizing
% Map", IEEE Transactions on Neural Networks, 2000.
% Contributed to SOM Toolbox vs2, February 2nd, 2000 by Esa Alhoniemi
% Copyright (c) by Esa Alhoniemi
% http://www.cis.hut.fi/projects/somtoolbox/
% ecco 301299 juuso 020200 211201
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%