发布时间:2024-11-16 16:01
sigma = X\' * X / m;
[U,S,V] = svd(sigma);
U_reduce = U(:,1:K);
Z = X * U_reduce;
U_reduce = U(:,1:K);
X_rec = Z * U_reduce\';
m = size(X,1);
for i = 1:m
ind_k = zeros(K,1);
for j = 1:K
ind_k(j) = sum((X(i,:) - centroids(j,:)) .^ 2);
endfor
[value,index] = min(ind_k);
idx(i) = index;
endfor
for i = 1:K
centroids(i,:) = ((idx == i)\' * X) / sum(idx == i);
endfor