-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.m
77 lines (54 loc) · 1.6 KB
/
main.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
function label = main(filename,K,sigma)
%% Load data
A = importdata(filename);
global D
D = A(:,[1,2]);
global N
N = size(D,1)
%sigma = 1;
%K = 5;
matrix_A = zeros(N,N);
for i=1:N
for j=1:N
distance_ij = euclidean_distance(D(i,:), D(j,:));
matrix_A(i, j) = exp(-distance_ij/(2*sigma^2));
if(i == j)
matrix_A(i, j) = 0;
end
end
end
%% calculate Degree matrix
matrix_D = zeros(N,N);
for i=1:N
matrix_D(i, i) = sum(matrix_A(i, :));
end
L = matrix_D - matrix_A;
%% calculate laplacian matrix
matrix_L = zeros(N,N);
for i=1:N
for j=1:N
matrix_L(i,j) = matrix_A(i,j) / (sqrt(matrix_D(i,i)) * sqrt(matrix_D(j,j)));
end
end
%% calculate eigenvalue and eigen vector
[eigenvectors, eigenvalues] = eig(matrix_L);
matrix_X = zeros(N, K);
size_eigenvectors = size(eigenvectors(1,:));
size_eigen = size_eigenvectors(1,2);
matrix_X(:, :) = eigenvectors(:, size_eigen-K+1:size_eigen);
%% calculate normalized matrix of eigenvector and run kmeans on it
matrix_Y = normc(matrix_X);
label = kmeansplus(matrix_Y,K);
[idx] = kmedoids(matrix_Y,K);
S = mykmeans(matrix_Y, K);
kmeanslabel = set_label(matrix_Y, S);
vols = CalculateClusterVolumes(idx,matrix_D,K);
ncut = solve_ncut(D, vols, idx, L, matrix_D);
visualize_result(D,idx,'kmedians',ncut,K);
%vols = CalculateClusterVolumes(label,matrix_D,K);
%ncut = solve_ncut(D, vols, label, L, matrix_D);
%visualize_result(D,label,'kmeans++',ncut,K);
%vols = CalculateClusterVolumes(kmeanslabel,matrix_D,K);
%ncut = solve_ncut(D, vols, kmeanslabel, L, matrix_D);
%visualize_result(D,kmeanslabel,'kmeans',ncut,K);
end