function u = cluster(x, k)
%	u = cluster(x, k);
%	x: 2 * N
%	u: 2 * k;

u = rand(2, k)*3-1.5;
idold = 0;

while 1,
	d = dist2(u, x);

	[ds, id] = min(d);
	if idold == id
		break;
	end
	idold = id;
	
	for i = 1:k
		idx = find(id == i);
		if isempty(idx)
			u(:, i) = rand(2,1) * 3 - 1.5;
			idold = 0;
		else
			u(:, i) = mean(x(:, idx),2);
		end
	end;
end
 
figure;
hold on;
clr = 'rbkcm';
for i = 1:k
	plot(x(1, find(id==i)), x(2, find(id==i)), [clr(i) 'o']);
	plot(u(1, i), u(2, i), [clr(i) '+']);
end;