-
Notifications
You must be signed in to change notification settings - Fork 0
/
Clustering.m
135 lines (125 loc) · 3.89 KB
/
Clustering.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
%Personal Setting
clc;
clear;
perimeter = 3; %thinkness of the perimeter. eg. 1 means there are 9 data points; 2 means there are 25 data points
numberOfCluster = 3;
%Log curve Fitting
load('RxPwrdBGrid.mat');
fittingResult = [];
for n = 1:60 %This figure has to be 60, as there are 60 seats in the carbin
%n is the transmitter
for m = 1:60 %This figure has to be 60, as there are 60 seats in the carbin
%m is the receiver
if n ~= m
seatXR = xyzSeats(m,2); %Seat coordinate which sends the signal
seatYR = xyzSeats(m,3);
[a,b, seatNumber] = fitting (perimeter, seatXR, seatYR, n); % n is used to find suitable cell in mat
oneFittingResult = [a,b,seatNumber,m];
fittingResult = [fittingResult; oneFittingResult];
end
end
disp ([n]);
end
save fittingResult.mat
%Clustering: Kmeans
load fittingResult
[idx, C, SUMD] = kmeans(fittingResult(:,1:2),numberOfCluster,'MaxIter',100,'Distance','cityblock');
finalResult = [idx fittingResult]
save finalResult
%{
%Clustering: hierarchical
load fittingResult
Y = pdist(fittingResult(:,1:2),'cityblock');
squareform(Y);
Z = linkage(Y,'average');
C = cophenet(Z,Y)
I = inconsistent(Z)
disp(dendrogram(Z))
T = cluster(Z,'maxclust',10);
finalResult = [T fittingResult];
%}
figure;
%plotting clustering
for n = 1:3540
if finalResult(n,1) == 1;
plot (finalResult(n,2),finalResult(n,3),'r*');
hold on;
elseif finalResult(n,1) == 2;
plot (finalResult(n,2),finalResult(n,3),'b*');
hold on;
elseif finalResult(n,1) == 3;
plot (finalResult(n,2),finalResult(n,3),'g*');
hold on;
elseif finalResult(n,1) == 4;
plot (finalResult(n,2),finalResult(n,3),'c*');
hold on;
elseif finalResult(n,1) == 5;
plot (finalResult(n,2),finalResult(n,3),'m*');
hold on;
else finalResult(n,1) == 6;
plot (finalResult(n,2),finalResult(n,3),'y*');
hold on;
end
end
hold on;
plot(C(:,1), C(:,2),'d','MarkerSize',5);
xlabel 'k1';
ylabel 'k2';
title 'Cluster: K1, K2';
hold off;
%{
% Calculating Cost Function
disp('SUMD');
disp(SUMD);
J = 0;
for n = 1: numberOfCluster
J = J + SUMD(n,1);
end
J = 1/3540 * J;
disp (J);
%}
%Plotting cluster on plane cabin
figure;
for n = 1:60
startNum = (n-1) * 59 + 1;
endNum = startNum + 59 - 1;
for m = startNum : endNum
if finalResult(m,1) == 1;
%plot (finalResult(n,2),finalResult(n,3),'k*');
seatNumber1 = finalResult(m,5);
plot (xyzSeats(seatNumber1,2),xyzSeats(seatNumber1,3),'r*');
%disp(seatNumber1);
hold on;
elseif finalResult(m,1) == 2;
seatNumber2 = finalResult(m,5);
plot (xyzSeats(seatNumber2,2),xyzSeats(seatNumber2,3),'b*');
%disp(seatNumber2);
hold on;
elseif finalResult(m,1) == 3;
seatNumber3 = finalResult(m,5);
plot (xyzSeats(seatNumber3,2),xyzSeats(seatNumber3,3),'g*');
%disp(seatNumber3);
hold on;
elseif finalResult(m,1) == 4;
seatNumber4 = finalResult(m,5);
plot (xyzSeats(seatNumber4,2),xyzSeats(seatNumber4,3),'k*');
%disp(seatNumber3);
hold on;
elseif finalResult(m,1) == 5;
seatNumber5 = finalResult(m,5);
plot (xyzSeats(seatNumber5,2),xyzSeats(seatNumber5,3),'c*');
%disp(seatNumber3);
hold on;
else finalResult(m,1) == 6;
seatNumber6 = finalResult(m,5);
plot (xyzSeats(seatNumber6,2),xyzSeats(seatNumber6,3),'y*');
%disp(seatNumber3);
hold on;
end
end
nStr = num2str(n);
hold off;
filename = strcat ('fig', nStr, '.png');
saveas (gcf,filename);
end
hold off;