new; % construct a multivariate (bivariate) normal distribution of size 100x2 % mean is 0 for both variables, covariance matrix is 2x2 % and vectors are 100 X = mvnrnd([0;0],[1 .9;.9 1],100); % construct a sampling grid %Y = [1 1;1 -1;-1 1;-1 -1]; Y = flipud([2 2;2 -2;-2 2;-2 -2;1.5 1.5;1.5 -1.5;-1.5 1.5;-1.5 -1.5;1 1;1 -1;-1 1;-1 -1;.5 .5;-.5 .5;.5 -.5;-.5 -.5]); ll = length( Y); % get tge Mahalanobis/Euclidean distance on the grid points d1 = mahal(Y,X) % Mahalanobis %d2 = sum((Y-repmat(mean(X),4,1)).^2, 2) % Squared Euclidean d2 = sum((Y-repmat(mean(X),ll,1)).^2, 2) % Squared Euclidean % generate a plot to show results scatter(X(:,1),X(:,2)) hold on % show Euclidean d2 scaled for illustration purposes scatter(Y(:,1),Y(:,2),100,d2*10,'s','LineWidth',2,'SizeData',200) scatter(Y(:,1),Y(:,2),100,d1,'*','LineWidth',2) hb = colorbar; ylabel(hb,'Distance scale') legend('X','Euclidean','Mahalanobis','Location','NW') grid on;