X=randn(2,100)*0.5+repmat([1 1]',1,100);
Y=randn(2,100)*0.5+repmat([-1 -1]',1,100);
plot(X(1,:),X(2,:),'r*');
hold on
plot(Y(1,:),Y(2,:),'b*');
Z=[X Y];
C=cov(Z',1);
[E D]=eig(C);
hold on
plot([-4*E(1,2) 4*E(1,2)],[-4*E(2,2) 4*E(2,2)],'-')
plot([-4*E(1,1) 4*E(1,1)],[-4*E(2,1) 4*E(2,1)],'-')
for i=1:1:100
VECTOR1=E(:,2)'*X(:,i)*E(:,2);
VECTOR2=E(:,2)'*Y(:,i)*E(:,2);
plot([X(1,i) VECTOR1(1)],[ X(2,i) VECTOR1(2)],'c-')
hold on
plot([Y(1,i) VECTOR2(1)],[ Y(2,i) VECTOR2(2)],'m-')
hold on
end
