scatter(X(:,1),X(:,2),10,'.') % Scatter plot with points of size 10
hold on
scatter(Y(:,1),Y(:,2),100,d2_mahal,'o','filled')
hb = colorbar;
ylabel(hb,'Mahalanobis Distance')
legend('X','Y','Location','best')
Y 中的所有观测值([1,1]、[-1,-1,]、[1,-1] 和 [-1,1])在欧几里德距离上与 X 均值等距。但在马氏距离上,[1,1] 和 [-1,-1] 比 [1,-1] 和 [-1,1] 更接近 X。由于马氏距离会考虑数据的协方差和不同变量的尺度,因此它对于检测离群值非常有用。