Utoljára aktív 1698157880

1.m Eredeti
1clear; clc;
2
3X = xlsread('DATA1B');
4X=X(1:13,1:4)
5[n,p] = size(X);
6
7k = 3;
8
9idx = [3 9 12];
10%
11C = X(idx,:)
12%
13temp = zeros(n,1);
14
15it=0
16while 1,
17 for i =1:n
18 for l = 1:k
19 sum=0;
20 for j = 1:p
21 sum=sum+(X(i,j)-C(l,j))^2;
22 end
23 d(i,l)=sqrt(sum);
24 end
25 end
26 [z,g]=min(d,[],2);
27 if(g==temp)
28 break;
29 else
30 temp = g;
31 end
32 for i=1:k
33 f=find(g==i);
34 if f
35 C(i,:)=mean(X(find(g==i),:),1);
36 end
37 end
38 it=it+1;
39end
40
41
42y = [X,g];
43for i=1:n
44 no(i) = norm(X(i,:));
45end
46y1 = [(1:n)', no',g];
47
48k1 = X(find(g==1),:)
49k2 = X(find(g==2),:)
50k3 = X(find(g==3),:)
51[n1,p1] = size(k1);
52[n2,p2] = size(k2);
53[n3,p3] = size(k3);
54for i=1:n1
55 norm1(i) = norm(k1(i,:));
56end
57
58x1 = [k1 norm1']
59
60for i=1:n2
61 norm2(i) = norm(k2(i,:));
62end
63
64x2 = [k2 norm2']
65
66for i=1:n3
67 norm3(i) = norm(k3(i,:));
68end
69
70x3 = [k3 norm3']
71
72cov1 = cov(k1)
73cov2 = cov(k2)
74cov3 = cov(k3)
75det1 = det(cov1)
76det2 = det(cov2)
77det3 = det(cov3)
78c1 = y1(find(g==1),:);
79c2 = y1(find(g==2),:);
80c3 = y1(find(g==3),:);
81
82plot(c1(:,1),norm1, 'r*', c2(:,1),norm2, 'b*', c3(:,1),norm3, 'g*')
83hold on
84grid on
85legend({'Cluster 1','Cluster 2', 'Cluster 3'},'Location','northeast')
86title('Norm Distance Plot');
2.m Eredeti
1clear; clc;
2
3% Get Data
4gbr1 = imread('images/Banana/1.jpg');
5gbr2 = imread('images/Banana/2.jpg');
6gbr3 = imread('images/Banana/3.jpg');
7gbr4 = imread('images/Banana/4.jpg');
8gbr5 = imread('images/Banana/5.jpg');
9gbr6 = imread('images/Banana/6.jpg');
10gbr7 = imread('images/Banana/7.jpg');
11gbr8 = imread('images/Banana/8.jpg');
12gbr9 = imread('images/Banana/9.jpg');
13gbr10 = imread('images/Banana/10.jpg');
14gbr11 = imread('images/Kiwi/1.jpg');
15gbr12 = imread('images/Kiwi/2.jpg');
16gbr13 = imread('images/Kiwi/3.jpg');
17gbr14 = imread('images/Kiwi/4.jpg');
18gbr15 = imread('images/Kiwi/5.jpg');
19gbr16 = imread('images/Kiwi/6.jpg');
20gbr17 = imread('images/Kiwi/7.jpg');
21gbr18 = imread('images/Kiwi/8.jpg');
22gbr19 = imread('images/Kiwi/9.jpg');
23gbr20 = imread('images/Kiwi/10.jpg');
24gbr21 = imread('images/Orange/1.jpg');
25gbr22 = imread('images/Orange/2.jpg');
26gbr23 = imread('images/Orange/3.jpg');
27gbr24 = imread('images/Orange/4.jpg');
28gbr25 = imread('images/Orange/5.jpg');
29gbr26 = imread('images/Orange/6.jpg');
30gbr27 = imread('images/Orange/7.jpg');
31gbr28 = imread('images/Orange/8.jpg');
32gbr29 = imread('images/Orange/9.jpg');
33gbr30 = imread('images/Orange/10.jpg');
34gbr1=double(rgb2gray(gbr1));
35gbr2=double(rgb2gray(gbr2));
36gbr3=double(rgb2gray(gbr3));
37gbr4=double(rgb2gray(gbr4));
38gbr5=double(rgb2gray(gbr5));
39gbr6=double(rgb2gray(gbr6));
40gbr7=double(rgb2gray(gbr7));
41gbr8=double(rgb2gray(gbr8));
42gbr9=double(rgb2gray(gbr9));
43gbr10=double(rgb2gray(gbr10));
44gbr11=double(rgb2gray(gbr11));
45gbr12=double(rgb2gray(gbr12));
46gbr13=double(rgb2gray(gbr13));
47gbr14=double(rgb2gray(gbr14));
48gbr15=double(rgb2gray(gbr15));
49gbr16=double(rgb2gray(gbr16));
50gbr17=double(rgb2gray(gbr17));
51gbr18=double(rgb2gray(gbr18));
52gbr19=double(rgb2gray(gbr19));
53gbr20=double(rgb2gray(gbr20));
54gbr21=double(rgb2gray(gbr21));
55gbr22=double(rgb2gray(gbr22));
56gbr23=double(rgb2gray(gbr23));
57gbr24=double(rgb2gray(gbr24));
58gbr25=double(rgb2gray(gbr25));
59gbr26=double(rgb2gray(gbr26));
60gbr27=double(rgb2gray(gbr27));
61gbr28=double(rgb2gray(gbr28));
62gbr29=double(rgb2gray(gbr29));
63gbr30=double(rgb2gray(gbr30));
64
65gbr1=reshape(gbr1',1,[]);
66gbr2=reshape(gbr2',1,[]);
67gbr3=reshape(gbr3',1,[]);
68gbr4=reshape(gbr4',1,[]);
69gbr5=reshape(gbr5',1,[]);
70gbr6=reshape(gbr6',1,[]);
71gbr7=reshape(gbr7',1,[]);
72gbr8=reshape(gbr8',1,[]);
73gbr9=reshape(gbr9',1,[]);
74gbr10=reshape(gbr10',1,[]);
75gbr11=reshape(gbr11',1,[]);
76gbr12=reshape(gbr12',1,[]);
77gbr13=reshape(gbr13',1,[]);
78gbr14=reshape(gbr14',1,[]);
79gbr15=reshape(gbr15',1,[]);
80gbr16=reshape(gbr16',1,[]);
81gbr17=reshape(gbr17',1,[]);
82gbr18=reshape(gbr18',1,[]);
83gbr19=reshape(gbr19',1,[]);
84gbr20=reshape(gbr20',1,[]);
85gbr21=reshape(gbr21',1,[]);
86gbr22=reshape(gbr22',1,[]);
87gbr23=reshape(gbr23',1,[]);
88gbr24=reshape(gbr24',1,[]);
89gbr25=reshape(gbr25',1,[]);
90gbr26=reshape(gbr26',1,[]);
91gbr27=reshape(gbr27',1,[]);
92gbr28=reshape(gbr28',1,[]);
93gbr29=reshape(gbr29',1,[]);
94gbr30=reshape(gbr30',1,[]);
95
96X = [gbr1; gbr2; gbr3; gbr4; gbr5; gbr6; gbr7; gbr8; gbr9; gbr10; gbr11; gbr12; gbr13; gbr14; gbr15; gbr16; gbr17; gbr18; gbr19; gbr20; gbr21; gbr22; gbr23; gbr24; gbr25; gbr26; gbr27; gbr28; gbr29; gbr30];
97[n,p] = size(X);
98Xrata = mean(X, 1);
99
100%1. mengurangi setiap variable dengan rata2nya (normalisasi data)
101Z = zeros(size(X));
102for i = 1:n
103 Z(i, :) = X(i, :)-Xrata;
104end
105
106%Menghitung covariance matrix
107S = cov(X); % varian
108
109%Standardization Data
110Zr = zeros(size(Z));
111for i = 1:n
112 for j = 1:p
113 Zr(i,j) = Z(i,j) / sqrt(S(j,j));
114 end
115end
116% Zr ADALAH HASIL DARI NORMALISASI DATA X (DATA AWAL)
117
118%2. Menghitung cov dari data yang sudah direduksi
119Zcov = cov(Zr); % matrix covarian dari variable hasil normalisasi Zr
120
121%3. Menghitung eigen value dan eigen vector dari Zcov
122[VA, DA] = eig(Zcov); % VA = Eigen Vector ; DA = Eigen Value
123
124%4. menghitung komponen utama
125% Hasil library matlab eig() --> hasil = diagonal utamanya urut dari kecil ke besar
126% yang diperlukan dari besar ke kecil
127%mengurutkan hasil eigen (DESC)
128m = 1;
129for i = p:-1:1
130 DA_urut(m,m) = DA(i,i);
131 VA_urut(m,:) = VA(:,i)';
132 m = m+1;
133end
134
135%menghitung komponen utama
136eValue = zeros(p, 1);
137for i = 1:p
138 eValue(i,1) = DA_urut(i,i);
139end
140
141Total = sum(eValue); % total varians dalam data asli yang dapat dijelaskan oleh seluruh komponen utama.
142
143% Proporsi kumulatif = mengambil data seberapa persen secara kumulatif yang
144% dianggap perlu
145PK = 0;
146komponen = 0; % jumlah komponen utama yang dipertahankan
147
148proporsiValue = zeros(p, 1);
149for j = 1:p
150 proporsiValue(j) = eValue(j,1) / Total; % mengukur seberapa besar kontribusi eigenvalue ke-j terhadap varians total data.
151 if(PK < 0.9) % Nilai PK tergantung peneliti --> 0.8
152 PK = PK + proporsiValue(j);
153 komponen = komponen + 1;
154 end
155end
156
157% Mengambil eigen vector dari komponen yang terpilih
158VA_Komponen = VA_urut(:,1:komponen);
159% W = variable komponen utama / principal component variable
160W = Zr * VA_Komponen; % W ADALAH FINAL DATA YANG DISEBUTKAN DI PDF
161
162plot3(W(:,1), W(:,2), W(:,3), '*'); % PK < 0.9
163%plot(W(:,1), W(:,2), '*'); % PK < 0.8
164grid on