Skip to content
Prev 323961 / 398503 Next

keep the centre fixed in K-means clustering

So you just want to compare the distances from each point of your new 
data to each of the Centres and assign the corresponding number of the 
centre as in:

clust <- apply(NewData, 1, function(x) which.min(colSums(x - tCentre)^2))))


but since the apply loop is rather long here for lots of new data, one 
may want to optimize the runtime for huge data and get:

tNewData <- t(NewData)
clust <- max.col(-apply(Centre, 1, function(x) colSums((x - tNewData)^2)))


Best,
Uwe Ligges
On 21.05.2013 13:19, HJ YAN wrote: