Contents

% Ng, A., Jordan, M., and Weiss, Y. (2002). On spectral clustering: analysis and an algorithm. In T. Dietterich,
% S. Becker, and Z. Ghahramani (Eds.), Advances in Neural Information Processing Systems 14
% (pp. 849 – 856). MIT Press.

% Original script belongs to Asad Ali
% GIK Institute of Engineering Sciences & Technology, Pakistan
% Email: asad_82@yahoo.com
% This is a modified version

% CONCEPT: Introduced the normalization process of similarity matrix(degree-1/2 A degree-1/2),
% eigenvectors orthonormal conversion and clustering by kmeans
clear all;
close all;

generate the data mxn matrix

[img1,img2,img3,y,x] = GenerateData();

tic;
for units=1:4

calculate the similarity / similarity matrix

    similarity = CalculateAffinity(img1,img2,img3);

compute the degree matrix

    degree = diag(size(similarity,1)-1,size(similarity,1)-1);
    for i=1:size(similarity,1)
        degree(i,i) = sum(similarity(i,:));
    end

compute the normalized laplacian

    nLaplacian = real(sqrtm(inv(degree)) * similarity * sqrtm(inv(degree)));

perform the eigen value decomposition

    [eigVectors,eigValues] = eig(nLaplacian);

select k largest eigen vectors

    k = 4;
    counter=1;
    for i=(size(eigVectors,2)-(k-1)): size(eigVectors,1)
            nEigVec(:,counter) = eigVectors(:,i);
        counter = counter + 1
    end

construct the normalized matrix U from the obtained eigen vectors

    for i=1:size(nEigVec,1)
        n = sqrt(sum(nEigVec(i,:).^2));
        U(i,:) = nEigVec(i,:) ./ n;
    end

perform kmeans clustering on the matrix U

    [IDX,C] = kmeans(real(U),k);
end
toc;