How to calculate all Euclidean distances between two sets of points in a 3-dimensional space

1,747 views
Skip to first unread message

Carlos Baptista

unread,
Jun 4, 2014, 7:44:09 AM6/4/14
to julia...@googlegroups.com
Let's say I have two sets of points in a 3-dimensional space represented by arrays of sizes Nx3 and Mx3. What is the most efficient way to calculate the distances between all points of set 1 against all points of set 2?

This is a (not so efficient example) of what I want to accomplish:


function euclidean_distance(pointA::AbstractMatrix, pointB::AbstractMatrix)
    N
= size(pointA, 1)
    M
= size(pointB, 1)

    D
= zeros(N, M)
   
for j = 1:M
       
for i = 1:N
            D
[i, j] = norm(pointA[i, :] - pointB[j, :])
       
end
   
end
   
   
return D
end

Miguel Bazdresch

unread,
Jun 4, 2014, 8:44:20 AM6/4/14
to julia...@googlegroups.com
Have you seen the Distance package?

https://github.com/JuliaStats/Distance.jl

-- mb

Carlos Baptista

unread,
Jun 4, 2014, 8:50:35 AM6/4/14
to julia...@googlegroups.com
Yes I have seen it. I tried something with the function pairwise, but I did not really get what I want. If I have two sets of sizes N and M respectively, I expect as return value a matrix of size NxM. What I got out of pairwise is smaller than that.

Dahua Lin

unread,
Jun 4, 2014, 9:15:51 AM6/4/14
to julia...@googlegroups.com
The Distance package assumes that each column is a point

Since Julia uses column-major layout for arrays, using column-major ways to organize your data would be more efficient in general. If you have a matrix comprised of points along rows, you can supply the transposed versions to the package's functions.

Dahua

paul analyst

unread,
Jun 4, 2014, 9:16:05 AM6/4/14
to julia...@googlegroups.com
It is there. If D aand D1 ar two arrays with vectors in columns You  have this in Distance package.

julia> r = pairwise(Euclidean(), D,D1)

4x4 Array{Float64,2}:
 4579.3   4528.87  2650.71  7326.62
 5658.24  3646.92  2587.83  7348.04
 5729.42  4581.69  1941.65  7266.23
 5733.62  4656.58  2488.6   5902.54

in paresecs too ;)
Paul

Dahua Lin

unread,
Jun 4, 2014, 9:16:36 AM6/4/14
to julia...@googlegroups.com

Carlos Baptista

unread,
Jun 6, 2014, 6:32:58 AM6/6/14
to julia...@googlegroups.com
Thanks. I had a look before posting. Guess I wasn't thorough enough. Thanks to your implementation of pairwise and Euclidean execution time was cut by 3 and memory usage cut by 4.
Reply all
Reply to author
Forward
0 new messages