第1个:通过迭代次数来决定算法终止
% K-Means Clustering
% idx: represent the indication vector
% center: stand for the clustering center
% data: the primal data matrix, which is a n by dim matrix
% k: the number of the clustering
% maxIter: the maximum number of iterative
% By Rong-Hua Li
function [idx , center] = Kmeans (data, center, k, maxIter)
[n, dim] = size (data);
% initial the indication vector, which the i-th element stands for the
% category corresponding the i-th row of the data
idx = zeros (n, 1);
% if the clustering center is empty , then select randomly k rows of the data matrix
% to initial the clustering center
if sum (size (center)) == 0
prek = randperm (n);
center = data (sort (prek (1:k)),:);
end
temp = zeros (k, 1);
for iter = 1 : maxIter
% computing the minmum (Euclidean distance, 2-norm) neighbor
for i = 1 : n
for j = 1 : k
temp(j) = norm (data (i, :) - center (j, :));
本内容试读结束,登录后可阅读更多
下载后可阅读完整内容,剩余3页未读,立即下载