Sample code for LDA
% Sample Code for Linear Discriminant Analysis
%C1 = [ 121 16.8 ; 114 15.2 ; 100 12.2 ; 120 13.1 ; 131 10.3];
C1 = [ 121 16.8 ; 114 15.2 ; 114 12.2 ; 120 13.1 ; 130 14.3];
%C2 = [ 210 9.4 ; 195 8.1 ; 182 8.0 ; 200 12.2 ; 190 14.4 ];
C2 = [ 200 9.4 ; 195 8.1 ; 192 12.0 ; 200 12.2 ; 190 14.4 ];
mu1 = [ sum(C1(:,1))/5 sum(C1(:,2))/5 ];
mu2 = [ sum(C2(:,1))/5 sum(C2(:,2))/5 ];
B = (mu1 - mu2)'*(mu1-mu2);
S1(1,1)=0;
S1(1,2)=0;
S1(2,1)=0;
S1(2,2)=0;
for i = 1:5
S1(1,1) = S1(1,1)+(C1(i,1)-mu1(1))*(C1(i,1)-mu1(1));
S1(1,2) = S1(1,2)+(C1(i,1)-mu1(1))*(C1(i,2)-mu1(2));
S1(2,1) = S1(2,1)+(C1(i,2)-mu1(2))*(C1(i,1)-mu1(1));
S1(2,2) = S1(2,2)+(C1(i,2)-mu1(2))*(C1(i,2)-mu1(2));
end
S2(1,1)=0;
S2(1,2)=0;
S2(2,1)=0;
S2(2,2)=0;
for i = 1:5
S2(1,1) = S2(1,1)+(C2(i,1)-mu2(1))*(C2(i,1)-mu2(1));
S2(1,2) = S2(1,2)+(C2(i,1)-mu2(1))*(C2(i,2)-mu2(2));
S2(2,1) = S2(2,1)+(C2(i,2)-mu2(2))*(C2(i,1)-mu2(1));
S2(2,2) = S2(2,2)+(C2(i,2)-mu2(2))*(C2(i,2)-mu2(2));
end
S = S1+S2;
SIN = inv(S);
SINB = SIN*B
eig(SINB)
[V, D] = eig(SINB);
V
m = V(1)/V(2);
CEN = (mu1 + mu2)/2;
CCC = CEN(2) - m*CEN(1);
for i =-10:10
y(i+11) = m*(i+CEN(1))+CCC;
x(i+11) = i+CEN(1);
end
plot(C1(:,1), C1(:,2), 'o' , C2(:,1), C2(:,2) , 'x' , x , y , mu1(1), mu1(2) , 'bv', mu2(1), mu2(2), 'rd', CEN(1), CEN(2), 'rs')