参考博客:https://blog.csdn.net/qq_20406597/article/details/80166589
X = load('data.txt');
True_number = find(X(:,3)==1);
False_number = find(X(:,3)==0);
X1 = X(True_number,1:2);
X0 = X(False_number,1:2);
hold on;
plot(X1(:,1),X1(:,2),'r+','markerfacecolor',[1,0,0]);
plot(X0(:,1),X0(:,2),'b*','markerfacecolor',[0,0,1]);
grid on;
%%%%求均值
M1 = mean(X1);
M0 = mean(X0);
M = mean([X1;X0]);
%%%%将需要的数据化为对应的矩阵形式,方便后续计算
p = size(X1,1);
q = size(X0,1);
M1_p = repmat(M1,p,1);
M0_q = repmat(M0,q,1);
M_p = repmat(M,p,1);
M_q = repmat(M,q,1);
%%%%计算类内散度矩阵
Sw1 = (X1-M1_p)'*(X1-M1_p);
Sw0 = (X0-M0_q)'*(X0-M0_q);
Sw = Sw1+Sw0;
%%%%计算类间散度矩阵
Sb1 = p*(M1_p-M_p)'*(M1_p-M_p);
Sb0 = q*(M0_q-M_q)'*(M0_q-M_q);
Sb = Sb1+Sb0;
%%%%求出最大特征值对应的最大特征向量
[vector,eigen] = eig(inv(Sw)*Sb);
[a,b] = max(max(eigen));
max_vector = vector(:,b);
%%%%求出直线方程
k = max_vector(2)/max_vector(1);
b = 0;
x = 0:1;
y = k*x + b;
hold on;
plot(x,y);
%%%%计算两类样本在直线上的投影
points1=zeros(p,2);
for i=1:p
x1 = (X1(i,1)+k*X1(i,2))/(k^2+1);
y1 = k*x1+b;
points1(i,1)=x1;
points1(i,2)=y1;
end
points0=zeros(q,2);
for i=1:q
x0 = (X0(i,1)+k*X0(i,2))/(k^2+1);
y0 = k*x0+b;
points0(i,1)=x0;
points0(i,2)=y0;
end
hold on;
plot(points1(:,1),points1(:,2),'r+','markerfacecolor',[1,0,0]);
plot(points0(:,1),points0(:,2),'b*','markerfacecolor',[0,0,1]);
0.697 0.460 1
0.774 0.376 1
0.634 0.264 1
0.608 0.318 1
0.556 0.215 1
0.403 0.237 1
0.481 0.149 1
0.437 0.211 1
0.666 0.091 0
0.243 0.267 0
0.245 0.057 0
0.343 0.099 0
0.639 0.161 0
0.657 0.198 0
0.360 0.370 0
0.593 0.042 0
0.719 0.103 0