A = load(‘Gaussian.txt‘); %A = load(‘ringData.txt‘); [m, n] = size(A); data=A(:,:); % markersize default is 6 %plot(data(:,1),data(:,2),‘.‘, ‘markersize‘, 10); % col sum. x^2 + y^2 squareData = sum(data.*data,2); transitData = data*data‘; % repmat means repeat matrix to 1*m xi = repmat(squareData,1,m); xj = repmat(squareData‘,m,1); E = 2*transitData-xi-xj; sig = 1; W = exp(E / (2 * sig * sig)); % set diagnoal to zeros W = W - diag(diag(W)); D = diag(sum(W,2)); %L = D-W; L =D^(-.5)*W*D^(-.5); K = 3; [X, di] = eig(L); [Xsort, Dsort] = eigsort(X, di); %Xsort = sort(X); Xuse = Xsort(:, 1:K); Xsq = Xuse.*Xuse; divmat=repmat(sqrt(sum(Xsq‘)‘),1,K); Y=Xuse./divmat; [c, Dsum, z] = kmeans(Y, K); kk = c; c1 = find(kk == 1); c2 = find(kk == 2); c3 = find(kk == 3); plot(data(c1,1),data(c1,2),‘.‘,‘marker‘, ‘+‘); hold on; plot(data(c2,1),data(c2,2),‘.‘,‘marker‘, ‘*‘); hold on; plot(data(c3,1),data(c3,2),‘.‘,‘marker‘, ‘+‘); function [Vsort, Dsort] = eigsort (V, D) eigvals = diag(D); [lohival,lohiindex] = sort(eigvals); lambda = flipud(lohival); index = flipud(lohiindex); Dsort = diag(lambda); % Sort eigenvectors to correspond to the ordered eigenvalues. Store sorted % eigenvectors as columns of the matrix vsort. M = length(lambda); Vsort = zeros(M,M); for i=1:M Vsort(:,i) = V(:,index(i)); end;
时间: 2024-10-11 04:38:24