首页 > 代码库 > LDA

LDA

clc;close all;clear all
c1=[2 2 2;
1 2 3];
c2=[4 5 6;
3 3 4];
c=[c1 c2];
plot(c1(1,:),c1(2,:),‘*‘,c2(1,:),c2(2,:),‘*‘);axis([0 8 0 8]);hold on
u=mean(c,2);
u1=mean(c1,2);
u2=mean(c2,2);

s1=zeros(2,2);
s2=zeros(2,2);
for i=1:3
s1=s1+(c1(:,i)-u1)*(c1(:,i)-u1)‘;
s2=s2+(c2(:,i)-u2)*(c2(:,i)-u2)‘;
end
SB=s1+s2;
w=inv(SB)*(u1-u2);
w=w/sqrt(sum(w‘*w));

v=[w(2);-w(1)];
b=-v‘*u;
x1=0:10;
x2=(-b-v(1)*x1)/v(2);
plot(x1,x2);hold on
c=c-[u u u u u u];
p=w‘*c;
x=w*p;
x=x+[u u u u u u]
plot(x(1,:),x(2,:),‘o‘)

LDA