Última actividad 1698157880

Revisión c25827f5993651a1fb14da76fdee0d26b557ee62

1.m Sin formato
1.
2.m Sin formato
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)
99
100%1. mengurangi setiap variable dengan rata2nya (normalisasi data)
101for i = 1:n
102 Z(i, :) = X(i, :)-Xrata;
103end
104
105%Menghitung covariance matrix
106S = cov(X); % varian
107
108%Standardization Data
109for i = 1:n
110 for j = 1:p
111 Zr(i,j) = Z(i,j) / sqrt(S(j,j));
112 end
113end
114%ZR ADALAH HASIL DARI NORMALISASI DATA X (DATA AWAL)
115
116%2. Menghitung cov dari data yang sudah direduksi
117Zcov = cov(Zr); % matrix covarian dari variable hasil normalisasi Zr
118
119%3. Menghitung eigen value dan eigen vector dari Zcov
120[VA, DA] = eig(Zcov); % VA = Eigen Vector ; DA = Eigen Value
121
122%4. menghitung komponen utama
123% Hasil library matlab eig() --> hasil = diagonal utamanya urut dari kecil ke besar
124% yang diperlukan dari besar ke kecil
125%mengurutkan hasil eigen (DESC)
126m = 1
127for i = p:-1:1
128 DA_urut(m,m) = DA(i,i);
129 VA_urut(m,:) = VA(i,:);
130 m = m+1;
131end
132
133%menghitung komponen utama
134for i = 1:p
135 eValue(i,1) = DA_urut(i,i);
136end
137
138Total = sum(eValue)% total varians dalam data asli yang dapat dijelaskan oleh seluruh komponen utama.
139
140% Proporsi kumulatif = mengambil data seberapa persen secara kumulatif yang
141% dianggap perlu
142PK = 0;
143komponen = 0;%jumlah komponen utama yang dipertahankan
144
145for j = 1:p
146 proporsiValue(j,:) = eValue(j,1) / Total;%mengukur seberapa besar kontribusi eigenvalue ke-j terhadap varians total data.
147 if(PK < 0.9) % Nilai PK tergantung peneliti --> 0.8
148 PK = PK + proporsiValue(j, :);
149 komponen = komponen + 1;
150 end
151end
152
153% Mengambil eigen vector dari komponen yang terpilih
154VA_Komponen = VA_urut(:,1:komponen)
155% W = variable komponen utama / principal component variable
156W = Z * VA_Komponen % W ADALAH FINAL DATA YANG DISEBUTKAN DI PDF
157
158plot3(W(:,1), W(:,2), W(:,3), '*') % PK < 0.9
159 %plot(W(:,1), W(:,2), '*') % PK < 0.8
160grid on