findClosestCentroids.m
m = size(X, 1); % m
for i = 1:m
dist = [];
for j = 1:K
dist(j) = sum((X(i, :)-centroids(j, :)) .^ 2);
end
[min_dist, min_idx] = min(dist);
idx(i) = min_idx;
end
computeCentroids.m
for i = 1:K
idx_set = find(i == idx);
ck = numel(idx_set);
if(0 ~= ck)
cen_sum = sum(X(idx_set, :));
centroids(i, :) = cen_sum / ck;
end
end
pca.m
Omega = X' * X / m;
[U S V] = svd(Omega);
projectData.m
Ureduce = U(:, 1:K);
x = X';
Z = x' * Ureduce; % i.e. X * Ureduce
recoverData.m
Ureduce = U(:, 1:K);
X_rec = Z * Ureduce';