Skip to content

Instantly share code, notes, and snippets.

@vtta
Created April 11, 2019 08:24
Show Gist options
  • Save vtta/5aad1e3839c77a5e4ab41685bbf0221f to your computer and use it in GitHub Desktop.
Save vtta/5aad1e3839c77a5e4ab41685bbf0221f to your computer and use it in GitHub Desktop.
simple k-means algorithm implemented in GNU Octave
function y = kMeans(m, k, i)
% m: input matrix
% row: each column is a sample
% column: sample number
% k: k clusters
% y: vector consist of the cluster number of each sample
[sampleNum, featureNum] = size(m);
centers = m(1:k, :);
y = zeros(sampleNum,1);
iterNum = 10000000;
cuttentIter = 0;
precision = 1e-8;
% endless loop
while 1
% calculate distance between every sample and every cluster center
distMatrix = zeros(sampleNum, k);
for i = 1:sampleNum
for j = 1:k
distMatrix(i,j) = norm(m(i,:)-centers(j,:), 2);
end
end
% update cluster according to distance
for i = 1:sampleNum
[~, y(i)] = min(distMatrix(i,:));
end
% update cluster centers
nSamplesInACluster = zeros(k,1);
lastCenters = centers;
centers = zeros(size(centers));
for i = 1:sampleNum
currentCluster = y(i);
centers(currentCluster,:) = centers(currentCluster,:).*nSamplesInACluster(currentCluster) + m(i,:);
nSamplesInACluster(currentCluster)=nSamplesInACluster(currentCluster)+1;
centers(currentCluster,:) = centers(currentCluster,:)./nSamplesInACluster(currentCluster);
end
% finish iteration condition
if(cuttentIter >= iterNum || max(abs(centers - lastCenters)) < precision )
break;
end
end % while
end % function kMeans
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment