# Get the Nth element of all vectors inside an array of vectors

**URL:** https://discourse.julialang.org/t/get-the-nth-element-of-all-vectors-inside-an-array-of-vectors/49742
**Category:** New to Julia
**Created:** [November 7, 2020, 3:42pm UTC](https://discourse.julialang.org/t/get-the-nth-element-of-all-vectors-inside-an-array-of-vectors/49742 "2020-11-07T15:42:32Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![GHRALIMA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ghralima/32/19244_2.png) [@GHRALIMA](https://discourse.julialang.org/u/GHRALIMA)
#### Post date: [November 7, 2020, 3:42pm UTC](https://discourse.julialang.org/t/get-the-nth-element-of-all-vectors-inside-an-array-of-vectors/49742/1 "2020-11-07T15:42:32Z")

</div>

I have a vector of vectors like:

a = [[1,2,3],[4,5,6],[7,8,9]]

When I want the first element of the first vector inside the vector, I simply use:

a[1][1]

But when I try to get the first element of all the vector using

a[:][1]

I get all the elements of the first vector

1  
2  
3

instead of the of

1  
4  
7

Is there any way to get the correct elements using something like the simplified notation above or using broadcasting, instead of using a for loop?

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [November 7, 2020, 3:52pm UTC](https://discourse.julialang.org/t/get-the-nth-element-of-all-vectors-inside-an-array-of-vectors/49742/2 "2020-11-07T15:52:39Z")

</div>

`getindex.(a,1)` should do what you want here. Unfortunately I don’t think there is a way to do this with `[]`

---

<div class="post-metadata">

### Author: ![roble](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roble/32/12303_2.png) [@roble](https://discourse.julialang.org/u/roble)
#### Post date: [November 7, 2020, 6:38pm UTC](https://discourse.julialang.org/t/get-the-nth-element-of-all-vectors-inside-an-array-of-vectors/49742/3 "2020-11-07T18:38:02Z")

</div>

> [@GHRALIMA](#):
>
> ts using something like the simplified notation above or using broadcasting, instead of using a for loop?

```julia
julia> a[1, :]
3-element Array{Int64,1}:
 1
 4
 7

julia> a[:, 1]
3-element Array{Int64,1}:
 1
 2
 3

```

Note that a[1][:] does not work. It first does a[1] which is the number 1. Later, it tries to access all numbers [:] of that single number, which results in an error.

---

<div class="post-metadata">

### Author: ![anon92994695](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@anon92994695](https://discourse.julialang.org/u/anon92994695)
#### Post date: [November 7, 2020, 6:55pm UTC](https://discourse.julialang.org/t/get-the-nth-element-of-all-vectors-inside-an-array-of-vectors/49742/4 "2020-11-07T18:55:29Z")

</div>

`getindex.(a,1)` works, but I’ll throw out some worthy mentions to share some of the fun of Julia.

`first.(a)`

or  
`map(x -> x[1],a)`

or

`[v[1] for v in a ]`

also work but may have performance penalties.

---

<div class="post-metadata">

### Author: ![GHRALIMA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ghralima/32/19244_2.png) [@GHRALIMA](https://discourse.julialang.org/u/GHRALIMA)
#### Post date: [November 7, 2020, 11:53pm UTC](https://discourse.julialang.org/t/get-the-nth-element-of-all-vectors-inside-an-array-of-vectors/49742/5 "2020-11-07T23:53:30Z")

</div>

Hi Roble,

That solution works when a is a 2D (3x3) array, which is a little bit different from what I asked. But, yes, I can convert my vector of vectors in an 2D array and that will work.

Thanks!

---

<div class="post-metadata">

### Author: ![GHRALIMA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ghralima/32/19244_2.png) [@GHRALIMA](https://discourse.julialang.org/u/GHRALIMA)
#### Post date: [November 7, 2020, 11:54pm UTC](https://discourse.julialang.org/t/get-the-nth-element-of-all-vectors-inside-an-array-of-vectors/49742/6 "2020-11-07T23:54:05Z")

</div>

Exactly what I was looking for! Thanks!
