一、 实验目的
二、实验仪器
三、实验内容
clear
iris=xlsread('C:\Users\admin\Desktop\iris.csv');
global meanx2 w1 w2 w3 meanx1 meanx3 test;
N=40;
for i=1:N
for j=1:4
w1(i,j)=iris(i,j+1);
end
end
sumx1=sum(w1,1);
for i=1:4
meanx1(1,i)=sumx1(1,i)/N;
end
for i=1:N
for j=1:4
w2(i,j)=iris(i+50,j+1);
end
end
sumx2=sum(w2,1);
for i=1:4
meanx2(1,i)=sumx2(1,i)/N;
end
for i=1:N
for j=1:4
w3(i,j)=iris(i+100,j+1);
end
end
sumx3=sum(w3,1);
for i=1:4
meanx3(1,i)=sumx3(1,i)/N;
end
z1(4,4)=0;
var1(4,4)=0;
for i=1:4
for j=1:4
for k=1:N
z1(i,j)=z1(i,j)+(w1(k,i)-meanx1(1,i))*(w1(k,j)-meanx1(1,j));
end
var1(i,j)=z1(i,j)/(N-1);
end
end
z2(4,4)=0;
var2(4,4)=0;
for i=1:4
for j=1:4
for k=1:N
z2(i,j)=z2(i,j)+(w2(k,i)-meanx2(1,i))*(w2(k,j)-meanx2(1,j));
end
var2(i,j)=z2(i,j)/(N-1);
end
end
z3(4,4)=0;
var3(4,4)=0;
for i=1:4
for j=1:4
for k=1:N
z3(i,j)=z3(i,j)+(w3(k,i)-meanx3(1,i))*(w3(k,j)-meanx3(1,j));
end
var3(i,j)=z3(i,j)/(N-1);
end
end
Var1_inv=[];
Var1_det=[];
Var2_inv=[];
Var2_det=[];
Var3_inv=[];
Var3_det=[];
var1_inv=inv(var1);
var2_inv=inv(var2);
var3_inv=inv(var3);
var1_det=det(var1);
var2_det=det(var2);
var3_det=det(var3);
M=10;
for i=1:M
for j=1:4
test(i,j)=iris(i+50,j+1);
end
end
t1=0;
t2=0;
t3=0;
for i=1:M
x=test(i,1);
y=test(i,2);
z=test(i,3);
h=test(i,4);
g1=(-0.5)*([x,y,z,h]-meanx1)*var1_inv*([x,y,z,h]'-meanx1')-0.5*log(abs(var1_det))+log(w1);
g2=(-0.5)*([x,y,z,h]-meanx2)*var2_inv*([x,y,z,h]'-meanx2')-0.5*log(abs(var2_det))+log(w2);
if g1>g2
t1=t1+1;
else
t2=t2+1;
end
end
plot3(w1,w2,w3)
- 实验结果
var1_inv =
18.8146 -13.3847 -6.4745 -5.9935
-13.3847 18.2505 5.9533 -7.4234
-6.4745 5.9533 39.4923 -19.3184
-5.9935 -7.4234 -19.3184 133.0217
var2_inv =
9.1333 -4.0764 -9.5825 9.6029
-4.0764 17.7118 3.6187 -19.9034
-9.5825 3.6187 23.0046 -31.5447
9.6029 -19.9034 -31.5447 86.5060
var3_inv =
12.0677 -4.8892 -12.1082 4.3864
-4.8892 14.8211 3.0037 -8.2567
-12.1082 3.0037 15.7867 -6.1219
4.3864 -8.2567 -6.1219 20.4719
var1_det =
1.5632e-06
var2_det =
2.2964e-05
var3_det =
1.4279e-04
t2 =
1
t2 =
2
t2 =
3
t2 =
4
t2 =
5
t2 =
6
t2 =
7
t2 =
8
t2 =
9
t2 =
10
左散点图 右折线图