-
Notifications
You must be signed in to change notification settings - Fork 0
/
Timing_SC_free_xor_KMeans.m
172 lines (162 loc) · 4.88 KB
/
Timing_SC_free_xor_KMeans.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
% X2 = readtable('C:\Users\mhashemi\OneDrive - Worcester Polytechnic Institute (wpi.edu)\Documents\MATLAB\GT.scv')
X_trans = X2_XOR';
size = 256;
% Y = pdist(X_trans(:,3));
% T = linkage(Y, 'complete');
% dendrogram(T)
for i = 1:length(X_trans)
f1(:,i) = X_trans(1:2:size,i);
f2(:,i) = X_trans(2:2:size,i);
end
%half gate with or
% for i = 1:length(X_trans)
% f1(:,i) = X_trans(1:3:size,i);
% f2(:,i) = X_trans(2:3:size,i);
% f3(:,i) = X_trans(3:3:size,i);
% end
% m = 1;
% % for i = length(X_trans)
% % for j = length(f2)
% % if(X_trans(3:3:size,i) == f2(:,j))
% % ind_f2(m) = i;
% % m = m + 1;
% % end
% % end
% % end
% for i = cast((length(X_trans)/3),"int8")
% ind_f2(i) = i*3;
% end
% f1 = X_trans;
% f1(:,ind_f2(i))=[];
% % for i = 1:length(X_trans)
% % n(1:2:5,i) = (X_trans(1:2:5,i)-min(reshape(f1,[64*3,1])))./(max(reshape(f1,[64*3,1]))-min(reshape(f1,[64*3,1])));
% % n(2:2:6,i) = (X_trans(2:2:6,i)-min(reshape(f2,[64*3,1])))./(max(reshape(f2,[64*3,1]))-min(reshape(f2,[64*3,1])));
% % end
normalization
for i = 1:length(X_trans)
n_n(1:2:size,i) = normalize(f1(:,i),'zscore','std');
n_n(2:2:size,i) = normalize(f2(:,i),'zscore','std');
% n_n(3:3:size,i) = normalize(f3(:,i),'scale','std');
end
%normalization
% for i = 1:length(X_trans)
% n_n(1:2:size,i) = normalize(f1(:,i),'zscore','std');
% n_n(2:2:size,i) = normalize(f2(:,i),'zscore','std');
% % n_n(3:3:size,i) = normalize(f3(:,i),'scale','std');
% end
% for i = 1:length(X_trans)
% n(1+1:2:size-1,i) = (f1(:,i)-min(f1(:,i)))./(max(f1(:,i))-min(f1(:,i)));
% n(2+1:2:size,i) = (f2(:,i)-min(f2(:,i)))./(max(f2(:,i))-min(f2(:,i)));
% end
% for i = 1:length(X_trans)
% n_all(1:size,i) = normalize(X_trans(:,i),'norm',1);
% end
% for i = 1:length(X_trans)
% n_mm_all(1:size,i) = (X_trans(:,i)-min(X_trans(:,i)))./(max(X_trans(:,i))-min(X_trans(:,i)));;
% end
% for i = 1:length(n)
% % test = reshape(X_trans_3d(i,:,:),[6,2])
% % Y = pdist(n(:,i));
% % T = linkage(Y, 'complete');
% % % idx(i,:) = cluster(T,'cutoff',0.24)';
% % idx(:,i) = cluster(T,'maxclust',2)';
% idx_n_n(:,i) = kmeans(n_n(:,i),2);
% % idx = reshape(idx,[64,6])
% % idx = cluster(T,"cutoff",10);
% % idx1(:,i) = knnsearch(X_trans(:,i),Y);
% end
% Y = pdist(n(:,4));
% T = linkage(Y, 'complete');
% dendrogram(T)
% for i = 1:length(X_trans)
% n(:,i) = (X_trans(:,i)-min(X_trans(:,i)))./(max(X_trans(:,i))-min(X_trans(:,i)));
% end
% Y = pdist(n(:,3));
% T = linkage(Y, 'complete');
% dendrogram(T)
% for i = 1:length(n)
% idx(:,i) = kmeans(n,2);
% end
% X = reshape(X_trans,[64*6,1])
% for i = 1:length(X)
% % for j = 1:length(X(1,:))
% % % if(mod(j,2)==1)
% % % X_trans_3d(i,j,1) = 1;
% % % end
% % % if(mod(j,2)==0)
% % % X_trans_3d(i,j,1) = 2;
% % % end
% % X_trans_3d(i,j,1) = X(i,j);
% % X_trans_3d(i,j,2) = X(i,j);
% % end
% X(i,2) = X(i,1);
% end
%'Options',opts,
opts = statset('Display','final');
for i = 1:length(X_trans)
n_ind(:,i) = n_n(1:size,i);
[idx(:,i),C] = kmeans(n_ind(:,i),2,'Distance','sqeuclidean','Replicates',10,'OnlinePhase', 'on','Options',opts,'start','plus');
end
binar = zeros(length(X_trans),size);
for i = 1:length(X_trans)
bb = 1;
in = Bin_XOR(i);
while in >= 1
binar(i,bb) = mod(in,2);
in = in - mod(in,2);
in = in / 2;
bb = bb + 1;
end
end
binar = binar';
zero_clust = 0;
one_clust = 0;
for i = 1:length(X_trans)
if (X_trans(1,i)<55000)
zero_clust = idx(1,i);
if(zero_clust == 1)
one_clust = 2;
else
one_clust = 1;
end
else
one_clust = idx(1,i);
if(one_clust == 1)
zero_clust = 2;
else
zero_clust = 1;
end
end
for j = 1 :size
if (binar(j,i) == 0)
binar_clusters(j,i) = zero_clust;
else
binar_clusters(j,i) = one_clust;
end
end
end
for i = 1 :length(X_trans)
correct_clust = 0;
for j = 1 : size
if(binar_clusters(j,i) == idx(j,i))
correct_clust = correct_clust + 1;
end
end
SR(i) = correct_clust/size;
end
SR_avg = mean(SR);
SR_min = min(SR);
% eva = evalclusters(n_ind,'kmeans','Silhouette','klist',[1:4]);
% noclusters=eva.OptimalK;
%
% n_ind = [n_ind,n_ind]
% figure;
% plot(n_ind(idx==1,1),n_ind(idx==1,2),'r.','MarkerSize',12)
% hold on
% plot(n_ind(idx==2,1),n_ind(idx==2,2),'b.','MarkerSize',12)
% plot(C(:,1),C(:,1),'kx',...
% 'MarkerSize',15,'LineWidth',3)
% legend('Cluster 1','Cluster 2','Centroids',...
% 'Location','NW')
% title 'Cluster Assignments and Centroids'
% hold off