findClosestCentroids
for i=1:size(X,1)
result = 99999;
for j=1:K
temp = sum((X(i, :) - centroids(j, :)) .^ 2);
if temp < result
result = temp;
idx(i) = j;
end
end
end
computeCentroids
for i = 1:K
temp = idx == i;
total = sum(X .* temp, 1); %We choose the X which belongs to the i th centroids
centroids(i, :) = total / sum(temp);
end
pca
Sigma = X' * X / size(X, 1);
[U S V] = svd(Sigma);
projectData
U_reduce = U(:, 1:K); %We select the top K most relevant features
Z = X * U_reduce;
recoverData
X_rec = Z * U(:, 1:K)';