# Distance matrix

**URL:** <https://discourse.julialang.org/t/distance-matrix/84619>\
**Category:** General Usage\
**Created:** [July 22, 2022, 4:58am UTC](https://discourse.julialang.org/t/distance-matrix/84619 "2022-07-22T04:58:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![JosieG](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josieg/32/36221_2.png) [@JosieG](https://discourse.julialang.org/u/JosieG)\
**Post date:** [July 22, 2022, 4:58am UTC](https://discourse.julialang.org/t/distance-matrix/84619/1 "2022-07-22T04:58:12Z")

</div>

I’m given a 20\*122 matrix p. Each row of matrix is a 20-element vector. I want to calculate the distance between each vector and form a distance matrix. Here’s my code

```julia
   mul = []
for i in 1:size(p,1)
    push!(mul,norm(p[1,:]-p[i,:]))
end
mul = transpose(tiedrank(mul))
for i in 2:size(p,1)
    for j in 1:size(p,1)
    mul2 = []
    push!(mul2,norm(p[i,:]-p[j,:]))
    end
    mul = vcat(mul,tiedrank(mul2)')
end
mul

```

I got the error that

> UndefVarError: mul2 not defined

How to fix this code?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [July 22, 2022, 5:08am UTC](https://discourse.julialang.org/t/distance-matrix/84619/2 "2022-07-22T05:08:51Z")

</div>

> [@JosieG](#):
>
> I’m given a 20\*122 matrix p. Each row of matrix is a 20-dim vector.

in this matrix each row has 122 elements, because

```julia
julia> rand(2,3)
2×3 Matrix{Float64}:
 0.589931 0.0452219 0.840308
 0.469587 0.824793 0.702456

```

also, is this some sort of homework problem? if so please indicate that in your post

---

<div class="post-metadata">

**Author:** ![JosieG](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josieg/32/36221_2.png) [@JosieG](https://discourse.julialang.org/u/JosieG)\
**Post date:** [July 22, 2022, 5:11am UTC](https://discourse.julialang.org/t/distance-matrix/84619/4 "2022-07-22T05:11:22Z")

</div>

Sorry for my typo, each row has 20 elements. This is not my hw though.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [July 22, 2022, 6:41am UTC](https://discourse.julialang.org/t/distance-matrix/84619/5 "2022-07-22T06:41:50Z")

</div>

Inside the `j` loop you create a local variable `mul2`, which isn’t accessible outside the loop. And even if it was, you reset it in every iteration. I would move the `mul2 = []` up one line as a first step.

---

<div class="post-metadata">

**Author:** ![JosieG](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josieg/32/36221_2.png) [@JosieG](https://discourse.julialang.org/u/JosieG)\
**Post date:** [July 22, 2022, 2:01pm UTC](https://discourse.julialang.org/t/distance-matrix/84619/6 "2022-07-22T14:01:39Z")

</div>

I tried it as

```julia
   mul = []
for i in 1:size(p,1)
    push!(mul,norm(p[1,:]-p[i,:]))
end
mul = transpose(tiedrank(mul))
for i in 2:size(p,1)
    for j in 1:size(p,1)
    mul2 = []
    push!(mul2,norm(p[i,:]-p[j,:]))
    end
    mul = vcat(mul,tiedrank(mul2)')
end
mul

```

But the value of elements in matrix are incorrect.

---

<div class="post-metadata">

**Author:** ![cgeoga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cgeoga/32/216186_2.png) [@cgeoga](https://discourse.julialang.org/u/cgeoga)\
**Post date:** [July 22, 2022, 2:44pm UTC](https://discourse.julialang.org/t/distance-matrix/84619/7 "2022-07-22T14:44:42Z")

</div>

Here is a more explicit hint of what @gustaphe is saying:

```julia
function test()
  broader_scope = Int64[] # you should type your containers, as an aside
  for j in 1:10
    narrower_scope = Int64[]
    push!(broader_scope, j)
    push!(narrower_scope, j)
  end
  # One of the two objects between broader_scope and narrower_scope can be used
  # here outside of the loop. Try uncommenting each of these lines and calling 
  # the test() function.
  # @show broader_scope
  # @show narrower_scope
end

```
