stepkm2.m

来自「计量工具箱」· M 代码 · 共 28 行

M
28
字号
function [center_index, obj_fcn, U] = stepkm(center_index, distmat)
%STEPKM One step in k-means clustering.
%	[CENTER, ERR] = STEPKM(CENTER, DATA)
%	performs one iteration of k-means clustering, where
%
%	DATA: matrix of data to be clustered. (Each row is a data point.)
%	CENTER: center of clusters. (Each row is a center.)
%	ERR: objective function for parititon U.

center_n = length(center_index);
data_n = size(distmat, 1);

% ====== Find the U (partition matrix)
[a,b] = min(distmat(center_index, :));
index = b+center_n*(0:data_n-1);
U = zeros(center_n, data_n);
U(index) = ones(size(index));

% ====== Find the new centers
for i = 1:center_n,
	data_index = find(U(i,:)==1);
	[junk, min_index] = min(sum(distmat(data_index, data_index)));
	center_index(i) = data_index(min_index); 
end

% ====== Find the new objective function
obj_fcn = sum(sum((distmat(center_index, :).^2).*U));	% objective function

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?