This document discusses different distance metrics including Euclidean distance, Manhattan distance, and Mahalanobis distance.
Euclidean distance is calculated as the square root of the summed squared differences along axes. Manhattan distance is the sum of the absolute differences of coordinates along axes. Mahalanobis distance accounts for correlations among variables by adjusting distances based on variable scales and correlations.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
27 views
Distances
This document discusses different distance metrics including Euclidean distance, Manhattan distance, and Mahalanobis distance.
Euclidean distance is calculated as the square root of the summed squared differences along axes. Manhattan distance is the sum of the absolute differences of coordinates along axes. Mahalanobis distance accounts for correlations among variables by adjusting distances based on variable scales and correlations.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2
To calculate the Euclidean distance one extracts the root of the summed squared differences along the two
along the two axes.
Thus, the distance between P1 and P2 is:
The Manhattan distance is defined as the sum of the absolute differences of the coordinates along the axes. As these differences are the same as for the latter distance measurement we sum them and the result for the Manhattan distance is:
In both cases the unit of the result is the number of blocks to pass. mandist Manhattan distance weight function expand all in page Syntax Z = mandist(W,P) D = mandist(pos)
Matlab norm((2-5)-(6-2),1). Norm (X_Y,1) or sum(abs((2-5)-(6-2))) sum(abs(X-Y)) f your two points are X and Y, the Manhattan distance is given by
d=norm(X-Y,1)
or even d = sum(abs(X-Y));
% Euclidean distance between vectors 'A' and 'B', original recipe EuclideanDistance = sqrt(sum( (A - B) .^ 2 ))
% Euclidean distance between vectors 'A' and 'B', linear algebra style EuclideanDistance = norm(A - B)
Mahalanobis Distance
The Mahalanobis distance takes into account the covariance among the variables in calculating distances. With this measure, the problems of scale and correlation inherent in the Euclidean distance are no longer an issue. To understand how this works, consider that, when using Euclidean distance, the set of points equidistant from a given location is a sphere. The Mahalanobis distance stretches this sphere to correct for the respective scales of the different variables, and to account for correlation among variables.
The mahal or pdist functions in the Statistics Toolbox can calculate the Mahalanobis distance. It is also very easy to calculate in base MATLAB. I must admit to some embarrassment at the simple-mindedness of my own implementation, once I reviewed what other programmers had crafted. See, for instance:
Note that it is common to calculate the square of the Mahalanobis distance. Taking the square root is generally a waste of computer time since it will not affect the order of the distances and any critical values or thresholds used to identify outliers can be squared instead, to save time.