Vectorize K-means

Problem

Let be a data matrix with examples in its columns (e.g. there are examples with features each). Let be a matrix of k-means centroids (cluster centers).

Recall the k-means algorithm iteratively computes two high-level steps:

  1. Find the distances from every example to every centroid.
  2. Update each centroid to be the mean of the examples assigned to it. The examples are assigned to the centroid which they are closest to.

Write a single line vectorized implementation of step 1.

Solution

show