Euclidean, Cosine and Angular Similarity and Distance

Version 1.1.0 (5.22 KB) by
Calculation of the Euclidean, cosine and angular similarity and distance between two sets of data.
Updated 10 Oct 2021

The present code is a set of three Matlab functions that provide calculation of six metrics:
- Euclidean distance, which vary in the range of [0 : Inf]. Larger values indicate high distance;
- Euclidean similarity, which vary in the range of [0 : 1]. Larger values indicate high similarity;
- cosine distance, which vary in the range of [0 : 2]. Larger values indicate high distance;
- cosine similarity, which vary in the range of [-1 : 1]. Larger values indicate high similarity;
- angular distance, which vary in the range of [0 : 1]. Larger values indicate high distance;
- angular similarity, which vary in the range of [0 : 1]. Larger values indicate high similarity.
The similarity and distance metrics are bound together – the smaller the distance is, the larger the similarity will get, and vice versa.
A few examples are given in order to clarify the usage of the functions. For convenience, the input and output arguments are given in the beginning of each function.
The code is based on the theory described in:
[1] T. Segaran. Programming Collective Intelligence, Ch. 2. Sebastopol, O’Reilly Media, 2007.
[2] E. Vezzetti, F. Marcolin. Similarity Measures for Face Recognition, Ch 5. Sharjah, Bentham Science Publishers, 2015.
[3] D. Cer, Y. Yang, S. Kong, N. Hua, N. Limtiaco, R. John, N. Constant, M. Guajardo-Céspedes, S. Yuan, C. Tar, Y. Sung, B. Strope and R. Kurzweil. Universal sentence encoder for English. Proceedings of the 2018 Conference on Empirical Methods in Natural Language Processing: System Demonstrations, pp. 169-174, 2018.

Cite As

Hristo Zhivomirov (2024). Euclidean, Cosine and Angular Similarity and Distance (https://www.mathworks.com/matlabcentral/fileexchange/98474-euclidean-cosine-and-angular-similarity-and-distance), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2017b
Compatible with any release
Platform Compatibility
Windows macOS Linux