% 2 dimensional Mahalonobis distance
clear
px=randn(1, 1000)*.8-1; py=randn(1,1000)*0.8-1;
qx=randn(1, 1000)+1; qy=randn(1,1000)+1;
plot(px,py,'bx');
Ap=cov(px,py)
hold on;
plot(qx,qy, 'gx');
Aq=cov(qx,qy)
xlabel('feature s1');
ylabel('feature s2');
hold on;
T_px=mean(px); T_py=mean(py);
T_qx=mean(qx); T_qy=mean(qy);
plot(T_px,T_py,'rs');
plot(T_qx,T_qy,'ro');
axis([-5,5,-5,5]);
grid on;
for x=-5:0.1:5
    for y=-5:0.1:5
        dp=abs([(x-T_px),(y-T_py)]* inv(Ap) *[(x-T_px),(y-T_py)]');
        dq=abs([(x-T_qx),(y-T_qy)]* inv(Aq) *[(x-T_qx),(y-T_qy)]');
        if abs(dp-dq)<0.2 plot(x,y,'k.');
        end
    end
end

hold off;
