# How to compute the correlation between elements in an array of arrays?

**URL:** https://discourse.julialang.org/t/how-to-compute-the-correlation-between-elements-in-an-array-of-arrays/35763
**Category:** New to Julia
**Tags:** question
**Created:** [March 9, 2020, 6:04pm UTC](https://discourse.julialang.org/t/how-to-compute-the-correlation-between-elements-in-an-array-of-arrays/35763 "2020-03-09T18:04:21Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![zxjroger](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zxjroger/32/11826_2.png) [@zxjroger](https://discourse.julialang.org/u/zxjroger)
#### Post date: [March 9, 2020, 6:04pm UTC](https://discourse.julialang.org/t/how-to-compute-the-correlation-between-elements-in-an-array-of-arrays/35763/1 "2020-03-09T18:04:21Z")

</div>

Suppose that I have the following array of vectors.

```julia
temp = Matrix{Vector{Float64}}(undef, 3, 1000)
for i = 1:3
    for j = 1:1000
        temp[i,j] = rand(10)
    end
end

```

Now I want to compute the correlation between the first element in the inner-vector and the second element in the inner-vector, separately for each row dimension of the outer-matrix. I was trying to do something like this:

```julia
cor(temp[1,:][1,:], temp[1,:][2,:])

```

But `temp[1,:][1,:]` only gives me the first vector , rather then collecting horizontally the first elements in all vectors in the first row of the outer-matrix.

---

<div class="post-metadata">

### Author: ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)
#### Post date: [March 9, 2020, 6:43pm UTC](https://discourse.julialang.org/t/how-to-compute-the-correlation-between-elements-in-an-array-of-arrays/35763/2 "2020-03-09T18:43:00Z")

</div>

You can define your own slicing function

```julia
julia> AoV_row(arr, row, idx) = getindex.(arr[row, :], idx)
AoV_row (generic function with 1 method)

julia> cor(AoV_row(temp,1,1), AoV_row(temp,1,2))
0.0036541528935823526

```

---

<div class="post-metadata">

### Author: ![zxjroger](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zxjroger/32/11826_2.png) [@zxjroger](https://discourse.julialang.org/u/zxjroger)
#### Post date: [March 9, 2020, 11:08pm UTC](https://discourse.julialang.org/t/how-to-compute-the-correlation-between-elements-in-an-array-of-arrays/35763/3 "2020-03-09T23:08:22Z")

</div>

Wow, this is much better than my for loop code. So, there is no direct way say `temp[i,j]` to do it. Thank you.

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [March 10, 2020, 6:38am UTC](https://discourse.julialang.org/t/how-to-compute-the-correlation-between-elements-in-an-array-of-arrays/35763/4 "2020-03-10T06:38:58Z")

</div>

If you want slicing to be more convenient you would be better off with a 3-dimensional array than a matrix of vectors.

---

<div class="post-metadata">

### Author: ![zxjroger](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zxjroger/32/11826_2.png) [@zxjroger](https://discourse.julialang.org/u/zxjroger)
#### Post date: [March 10, 2020, 5:15pm UTC](https://discourse.julialang.org/t/how-to-compute-the-correlation-between-elements-in-an-array-of-arrays/35763/5 "2020-03-10T17:15:53Z")

</div>

Thanks, I will also think about it.
