clear;
clc;
N=29;w=4;n=3;N1=4;N2=7;N3=8;N4=10;
A=[864.45 877.88 1418.79 1449.58;1647.31 2031.66 1775.89 1641.58;2665.9 3071.18 2772.9 3045.12]; % A belongs to w1
B=[2352.12 2297.28 2092.62 2205.36 2949.16 2802.88 2063.54
2557.04 3340.14 3177.21 3243.74 3244.44 3017.11 3199.76
1411.53 535.62 584.32 1202.69 662.42 1984.98 1257.21]; %B belongs to w2
C=[1739.94 1756.77 1803.58 1571.17 1845.59 1692.62 1680.67 1651.52
1675.15 1652 1583.12 1731.04 1918.81 1867.5 1575.78 1713.28
2395.96 1514.98 2163.05 1735.33 2226.49 2108.97 1725.1 1570.38]; %C belongs to w3
D=[373.3 222.85 401.3 363.34 104.8 499.85 172.78 341.59 291.02 237.63
3087.05 3059.54 3259.94 3477.95 3389.83 3305.75 3084.49 3076.62 3095.68 3077.78
2429.47 2002.33 2150.98 2462.86 2421.83 3196.22 2328.65 2438.63 2088.95 2251.96]; % D belongs to w4
X1=mean(A')'
X2=mean(B')'
X3=mean(C')'
X4=mean(D')' % mean of training samples for each category
S1=cov(A')
S2=cov(B')
S3=cov(C')
S4=cov(D') % covariance matrix of training samples for each type
S1_=inv(S1)
S2_=inv(S2)
S3_=inv(S3)
S4_=inv(S4)% inverse matrix of training samples for each type
S11=det(S1)
S22=det(S2)
S33=det(S3)
S44=det(S4) % determinant of convariance matrix
Pw1=N1/N
Pw2=N2/N
Pw3=N3/N
Pw4=N4/N
%Priori probability
sample=[1702.8 1639.79 2068.74
1877.93 1860.96 1975.3
867.81 2334.68 2535.1
1831.49 1713.11 1604.68
460.69 3274.77 2172.99
2374.98 3346.98 975.31
2271.89 3482.97 946.7
1783.64 1597.99 2261.31
198.83 3250.45 2445.08
1494.63 2072.59 2550.51
1597.03 1921.52 2126.76
1598.93 1921.08 1623.33
1243.13 1814.07 3441.07
2336.31 2640.26 1599.63
354 3300.12 2373.61
2144.47 2501.62 591.51
426.31 3105.29 2057.8
1507.13 1556.89 1954.51
343.07 3271.72 2036.94
2201.94 3196.22 935.53
2232.43 3077.87 1298.87
1580.1 1752.07 2463.04
1962.4 1594.97 1835.95
1495.18 1957.44 3498.02
1125.17 1594.39 2937.73
24.22 3447.31 2145.01
1269.07 1910.72 2701.97
1802.07 1725.81 1966.35
1817.36 1927.4 2328.79
1860.45 1782.88 1875.13];
% Posterior probability as the following
for k=1:30
P1=-1/2*(sample(k,:)'-X1)'*S1_*(sample(k,:)'-X1)+log(Pw1)-1/2*log(S11);
P2=-1/2*(sample(k,:)'-X2)'*S2_*(sample(k,:)'-X2)+log(Pw2)-1/2*log(S22);
P3=-1/2*(sample(k,:)'-X3)'*S3_*(sample(k,:)'-X3)+log(Pw3)-1/2*log(S33);
P4=-1/2*(sample(k,:)'-X4)'*S4_*(sample(k,:)'-X4)+log(Pw4)-1/2*log(S44);
P=[ P1 P2 P3 P4]
Pmax=max(P)
if P1==max(P)
w=1
plot3(sample(k,1),sample(k,2),sample(k,3),'ro');grid on;hold on;
elseif P2==max(P)
w=2
plot3(sample(k,1),sample(k,2),sample(k,3),'b>');grid on;hold on;
elseif P3==max(P)
w=3
plot3(sample(k,1),sample(k,2),sample(k,3),'g+');grid on;hold on;
elseif P4==max(P)
w=4
plot3(sample(k,1),sample(k,2),sample(k,3),'y*');grid on;hold on;
else
return
end
end
X1 =
1.0e+03 *
1.152675000000000
1.774110000000000
2.888774999999999
X2 =
1.0e+03 *
2.394708571428571
3.111348571428572
1.091252857142857
X3 =
1.0e+03 *
1.717732500000000
1.714585000000000
1.930032500000000
X4 =
1.0e+03 *
0.300846000000000
3.191462999999999
2.377188000000000
S1 =
1.0e+05 *
1.058519049666666 -0.243672190666667 0.098992338333333
-0.243672190666667 0.333258706000000 0.181040991333333
0.098992338333333 0.181040991333333 0.402718747666666
S2 =
1.0e+05 *
1.203515706476190 -0.062665951190476 0.407666054714286
-0.062665951190476 0.693132576142857 -0.749859064952381
0.407666054714286 -0.749859064952381 2.818153461904762
S3 =
1.0e+05 *
0.076576230214286 0.014979922000000 0.153577252500000
0.014979922000000 0.153407419428571 0.129408567714286
0.153577252500000 0.129408567714286 1.104023086214286
S4 =
1.0e+05 *
0.139466530266667 0.031677262800000 0.210403123244444
0.031677262800000 0.238012203566667 0.217209130288889
0.210403123244444 0.217209130288889 1.088291396844444
S1_ =
1.0e-04 *
0.141949039856477 0.162407741955397 -0.107902416351734
0.162407741955397 0.582841721124659 -0.301936192012301
-0.107902416351734 -0.301936192012301 0.410570257743875
S2_ =
1.0e-04 *
0.087696752125058 -0.008138192696592 -0.014851386584057
-0.008138192696592 0.203344798764831 0.055283577590749
-0.014851386584057 0.055283577590749 0.052342535624544
S3_ =
1.0e-03 *
0.181335101180276 0.003963731725881 -0.025689578253096
0.003963731725881 0.072425266359282 -0.009040743023059
-0.025689578253096 -0.009040743023059 0.013691094543604
S4_ =
1.0e-03 *
0.101789647104775 0.005394613449394 -0.020756039261065
0.005394613449394 0.051657518428909 -0.011353143289656
-0.020756039261065 -0.011353143289656 0.015467495117128
S11 =
7.145781915040528e+13
S22 =
1.586222200955639e+15
S33 =
8.416393420494519e+12
S44 =
2.081221747302809e+13
Pw1 =
0.137931034482759
Pw2 =
0.241379310344828
Pw3 =
0.275862068965517
Pw4 =
0.344827586206897
P =
1.0e+02 *
-0.347512458556192 -0.377619298765584 -0.166743942325226 -1.711604042361394
Pmax =
-16.674394232522630
w =
3
P =
1.0e+02 *
-0.495808074347476 -0.320756112513402 -0.191319317589630 -1.857205886142304
Pmax =
-19.131931758962985
w =
3
P =
1.0e+02 *
-0.325380368974539 -0.368428747683632 -1.058244568535408 -0.489684173722980
Pmax =
-32.538036897453921
w =
1
P =
1.0e+02 *
-0.615273016495049 -0.366997527249912 -0.190122730734145 -1.960725427899044
Pmax =
-19.012273073414498
w =
3
P =
1.0e+02 *
-1.076977428776064 -0.429981684653266 -2.446344473636417 -0.191425237379292
Pmax =
-19.142523737929174
w =
4
P =
1.0e+02 *
-3.231237183696745 -0.193721993016539 -1.925328987369824 -3.157399127375044
Pmax =
-19.372199301653872
w =
2
P =
1.0e+02 *
-3.440689717572647 -0.201602015137132 -1.974786253508708 -2.985021050872763
Pmax =
-20.160201513713236
w =
2
P =
1.0e+02 *
-0.288734580500097 -0.379473775523955 -0.175636933742260 -1.827101013591945
Pmax =
-17.563693374225991
w =
3
P =
1.0e+02 *
-0.842886376261127 -0.507629045333813 -3.162804246123065 -0.171190135250317
Pmax =
-17.119013525031711
w =
4
P =
1.0e+02 *
-0.296604835376749 -0.318272160822555 -0.291895398730490 -1.121974885250908
Pmax =
-29.189539873049021
w =
3
P =
1.0e+02 *
-0.399949922160553 -0.325544188971303 -0.194479953664775 -1.382933382255276
Pmax =
-19.447995366477528
w =
3
P =
1.0e+02 *
-0.656213294338128 -0.332002608405710 -0.191754725963717 -1.487787547165202
Pmax =
-19.175472596371659
w =
3
P =
1.0e+02 *
-0.231507614692569 -0.422484727448799 -0.694563665246198 -1.081710555995318
Pmax =
-23.150761469256928
w =
1
P =
1.0e+02 *
-1.506823168035746 -0.205668665361725 -0.929234211936244 -2.617162702691891
Pmax =
-20.566866536172515
w =
2
P =
1.0e+02 *
-0.952728849113573 -0.473863371529697 -2.777827392501171 -0.168863473654462
Pmax =
-16.886347365446191
w =
4
P =
1.0e+02 *
-2.354394287960419 -0.250041374909669 -0.929043863540774 -2.738237743250796
Pmax =
-25.004137490966947
w =
2
P =
1.0e+02 *
-0.986750407631300 -0.411397121008969 -2.330440265977830 -0.186407830660849
Pmax =
-18.640783066084932
w =
4
P =
1.0e+02 *
-0.343114313035573 -0.414902545740842 -0.213934645115504 -1.529499654841820
Pmax =
-21.393464511550441
w =
3
P =
1.0e+02 *
-1.142255531664389 -0.439679340341178 -2.691704268553619 -0.181769227932474
Pmax =
-18.176922793247357
w =
4
P =
1.0e+02 *
-2.932195967044705 -0.191167592080381 -1.522278792058808 -2.734274695880629
Pmax =
-19.116759208038115
w =
2
P =
1.0e+02 *
-2.316066239249319 -0.191683193464180 -1.291232679381424 -2.562709417076466
Pmax =
-19.168319346418016
w =
2
P =
1.0e+02 *
-0.244898591652870 -0.359915332626235 -0.215651110406853 -1.424473067981293
Pmax =
-21.565111040685338
w =
3
P =
1.0e+02 *
-0.474226464594098 -0.382726907505632 -0.225483308033057 -2.195497646101166
Pmax =
-22.548330803305678
w =
3
P =
1.0e+02 *
-0.227586655698655 -0.381842020531792 -0.449330460662646 -1.180111808022854
Pmax =
-22.758665569865546
w =
1
P =
1.0e+02 *
-0.192873617028170 -0.447371806232734 -0.721962623729349 -1.127623480577574
Pmax =
-19.287361702816991
w =
1
P =
1.0e+02 *
-1.177652290945027 -0.539294820946693 -3.795943927020383 -0.213596680456660
Pmax =
-21.359668045666027
w =
4
P =
-20.550821889462817 -36.825065884124712 -47.071600201823159 -98.798429553919632
Pmax =
-20.550821889462817
w =
1
P =
1.0e+02 *
-0.430681233913714 -0.353828466209347 -0.167483303119652 -1.819830447802539
Pmax =
-16.748330311965223
w =
3
P =
1.0e+02 *
-0.364517997189849 -0.310477375895759 -0.180932137165613 -1.652229329526691
Pmax =
-18.093213716561252
w =
3
P =
1.0e+02 *
-0.506917932386982 -0.340119629919988 -0.184785605256352 -1.897614735727016
Pmax =
-18.478560525635178
w =
3
|