VECTOR_L2
Returns the Euclidean, or L2, distance between two n-dimensional vectors.
Returns the Euclidean, or L2, distance between two n-dimensional vectors.
Behavior type
ImmutableSyntax
VECTOR_L2( vector_arr1, vector_arr2 )
Arguments
vector_arr1- N-dimensional vector, type ARRAY of INT, FLOAT, NUMERIC
vector_arr2- N-dimensional vector, type ARRAY of INT, FLOAT, NUMERIC
Note
NULL and NaN values are treated equivalently. If any element in the input array is NULL or NaN, the function returns NULL.Returns
FLOAT
Examples
The following example computes the distance for two vector arrays:
SELECT id, vector_arr1, vector_arr2, VECTOR_L2(vector_arr1, vector_arr2) AS distance FROM vectors_array;
id | vector_arr1 | vector_arr2 | distance
----+---------------+---------------+------------------
1 | [1.0,2.0,3.0] | [4.0,5.0,6.0] | 5.19615242270663
2 | [1.5,2.5,3.5] | [4.5,5.5,6.5] | 5.19615242270663
3 | [0.0,1.0,0.0] | [0.0,0.0,1.0] | 1.4142135623731
4 | [2.0,0.0,0.0] | [0.0,2.0,0.0] | 2.82842712474619
5 | [1.0,1.0,1.0] | [1.0,1.0,1.0] | 0
(5 rows)
The following example computes the distance between two 5D vectors:
SELECT VECTOR_L2(Array[1.0, 2.0, 3.0, 4.0, 5.0], Array[1.0, 2.0, 3.0, 3.0, 4.0]);
VECTOR_L2
-----------------
1.4142135623731
(1 row)