# Pairwise distances from a single column or vector

**URL:** <https://discourse.julialang.org/t/pairwise-distances-from-a-single-column-or-vector/29415>\
**Category:** New to Julia\
**Created:** [October 2, 2019, 3:44pm UTC](https://discourse.julialang.org/t/pairwise-distances-from-a-single-column-or-vector/29415 "2019-10-02T15:44:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jessecm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jessecm/32/25190_2.png) [@jessecm](https://discourse.julialang.org/u/jessecm)\
**Post date:** [October 2, 2019, 3:44pm UTC](https://discourse.julialang.org/t/pairwise-distances-from-a-single-column-or-vector/29415/1 "2019-10-02T15:44:41Z")

</div>

I’m a new Julia user, so this might be really obvious, but is there a straight-forward way to generate a pairwise distance matrix from a single vector or column?  
In R, it’s pretty straightforward:  
for the vector c(2, 4, 6, 8)

> dist(c(2, 4, 6, 8))  
> 1 2 3  
> 2 2  
> 3 4 2  
> 4 6 4 2

I’ve played around with Distances.jl, but I’m having trouble getting the function pairwise to work even for simple instances, e.g.:

using Distances  
cc = [2, 4, 6, 8];  
mm = [1, 3, 5, 7];  
pairwise(Euclidean(), cc)

ERROR: MethodError: no method matching pairwise(::Euclidean, ::Array{Int64,1})

pairwise(Euclidean(), cc, mm)

ERROR: MethodError: no method matching pairwise(::Euclidean, ::Array{Int64,1}, ::Array{Int64,1})

The non-pairwise version works. This obviously isn’t what I’m going for, but at least it demonstrates that some of the functionality of Distances.jl is up and running:  
euclidean(cc, mm)  
2.0

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [October 2, 2019, 3:48pm UTC](https://discourse.julialang.org/t/pairwise-distances-from-a-single-column-or-vector/29415/2 "2019-10-02T15:48:23Z")

</div>

I don’t know enough R to know exactly what you’re expecting, but shouldn’t the distance matrix be a 4x4 matrix? If that’s what you want, then it’s easy to write a simple Julia function to do this for you using a list comprehension:

```julia
julia> using LinearAlgebra: norm

julia> c = [2, 4, 6, 8]
4-element Array{Int64,1}:
 2
 4
 6
 8
                                                                                                                                                                                                                                                                                                                                      
julia> [norm(c[i] - c[j]) for i in eachindex(c), j in eachindex(c)]
4×4 Array{Int64,2}:                                                                                                                                                                                                                                                                                                                   
 0 2 4 6                                                                                                                                                                                                                                                                                                                           
 2 0 2 4
 4 2 0 2
 6 4 2 0

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [October 2, 2019, 4:13pm UTC](https://discourse.julialang.org/t/pairwise-distances-from-a-single-column-or-vector/29415/3 "2019-10-02T16:13:45Z")

</div>

In Distances, a collection of points (even if the points are one dimensional) is given as a matrix, not a vector. So either create it as a matrix or use the adjoint operator `'` to give it an extra dimension.

```julia
julia> pairwise(Euclidean(), cc'; dims=2)
4×4 Array{Float64,2}:
 0.0 2.0 4.0 6.0
 2.0 0.0 2.0 4.0
 4.0 2.0 0.0 2.0
 6.0 4.0 2.0 0.0

```

---

<div class="post-metadata">

**Author:** ![jessecm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jessecm/32/25190_2.png) [@jessecm](https://discourse.julialang.org/u/jessecm)\
**Post date:** [October 2, 2019, 4:27pm UTC](https://discourse.julialang.org/t/pairwise-distances-from-a-single-column-or-vector/29415/4 "2019-10-02T16:27:52Z")

</div>

Thanks very much! I’m hoping to apply this method to large numbers of large numbers, so the implementation suggested by kristoffer.carlsson below might be faster

re: output: the output in R is in the “lower triangle” format with no zeros, so it’s just:  
2  
4 2  
6 4 2

that’s eventually the format I’ll need things in, but I can play around with converting the output of your function. I’m pretty sure there are quick and dirty tricks to convert among distance matrix types

---

<div class="post-metadata">

**Author:** ![jessecm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jessecm/32/25190_2.png) [@jessecm](https://discourse.julialang.org/u/jessecm)\
**Post date:** [October 2, 2019, 9:06pm UTC](https://discourse.julialang.org/t/pairwise-distances-from-a-single-column-or-vector/29415/5 "2019-10-02T21:06:46Z")

</div>

Thanks very much for this response! Just to tack on for any future newbies who stumble upon this, if you want to generate a pairwise distance matrix from just one column of a 2d matrix, you can do:

```julia
julia> ccc = [2 4 6 ; 4 8 16 ; 6 12 18; 8 16 64]; 
julia> pairwise(Euclidean(), ccc[: , 1]'; dims = 2)
4×4 Array{Float64,2}:
 0.0 2.0 4.0 6.0
 2.0 0.0 2.0 4.0
 4.0 2.0 0.0 2.0
 6.0 4.0 2.0 0.0

```

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [October 2, 2019, 10:14pm UTC](https://discourse.julialang.org/t/pairwise-distances-from-a-single-column-or-vector/29415/6 "2019-10-02T22:14:41Z")

</div>

You can still use Robin’s answer, which I actually prefer, in a more simplified way:

```julia
julia> c = [2.0, 4, 6, 8]
4-element Array{Float64,1}:
 2.0
 4.0
 6.0
 8.0

julia> [abs(i-j) for i in c, j in c]
4×4 Array{Float64,2}:
 0.0 2.0 4.0 6.0
 2.0 0.0 2.0 4.0
 4.0 2.0 0.0 2.0
 6.0 4.0 2.0 0.0

```

---

<div class="post-metadata">

**Author:** ![mbaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbaz/32/17295_2.png) [@mbaz](https://discourse.julialang.org/u/mbaz)\
**Post date:** [October 2, 2019, 10:18pm UTC](https://discourse.julialang.org/t/pairwise-distances-from-a-single-column-or-vector/29415/7 "2019-10-02T22:18:39Z")

</div>

There is also [Distances.jl](https://github.com/JuliaStats/Distances.jl).

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [October 2, 2019, 10:30pm UTC](https://discourse.julialang.org/t/pairwise-distances-from-a-single-column-or-vector/29415/8 "2019-10-02T22:30:05Z")

</div>

Yes, I know, it’s a matter of preference, I don’t like to use a package unless I’m doing serious work with it.
