# Array of arrays indexing problem

**URL:** <https://discourse.julialang.org/t/array-of-arrays-indexing-problem/50145>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 14, 2020, 2:15pm UTC](https://discourse.julialang.org/t/array-of-arrays-indexing-problem/50145 "2020-11-14T14:15:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![amael](https://avatars.discourse-cdn.com/v4/letter/a/e95f7d/32.png) [@amael](https://discourse.julialang.org/u/amael)\
**Post date:** [November 14, 2020, 2:15pm UTC](https://discourse.julialang.org/t/array-of-arrays-indexing-problem/50145/1 "2020-11-14T14:15:37Z")

</div>

Hello,  
If i define an array of arrays like this:

```julia
a = [zeros(10) for i in 1:3]

```

I get an array of 3 elements that are arrays of 10 elements each. Then If I call

```julia
a[2][:]

```

I get an array of length 10 which is normal and fine. But If I call

```julia
a[:][1]

```

I also get an array of length 10 when I expect to have an array of length 3 composed of the first element of each arrays stored in a.  
In fact using “:” in one way (a[n][:]) or another (a[:][n]) always gives me back the same array.

Is this an expected behavior and I missed something or simply a bug ?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [November 14, 2020, 2:49pm UTC](https://discourse.julialang.org/t/array-of-arrays-indexing-problem/50145/2 "2020-11-14T14:49:50Z")

</div>

Here, `a[:]` is just a syntax to `copy` `a`. So `a[:][1]` gets the first element of `a`, which has 10 elements.

You want `getindex.(a, 1)` which applies the function `t -> getindex(t, 1)` to every element of `a`.

---

<div class="post-metadata">

**Author:** ![amael](https://avatars.discourse-cdn.com/v4/letter/a/e95f7d/32.png) [@amael](https://discourse.julialang.org/u/amael)\
**Post date:** [November 14, 2020, 3:01pm UTC](https://discourse.julialang.org/t/array-of-arrays-indexing-problem/50145/3 "2020-11-14T15:01:02Z")

</div>

Ok, `a[:]` is not simply an indexing operation. Thanks.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [November 14, 2020, 3:12pm UTC](https://discourse.julialang.org/t/array-of-arrays-indexing-problem/50145/4 "2020-11-14T15:12:49Z")

</div>

It behaves the same as other indexing. `a[:]` is the same as `a[[1, 2, 3]]` in that it makes a copy of the elements. See below

```julia
julia> t = [6, 7, 8, 9];

julia> s = t[[1, 2, 3]]
3-element Array{Int64,1}:
 6
 7
 8

julia> s[1] = 1000
1000

julia> t
4-element Array{Int64,1}:
 6
 7
 8
 9

```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [November 14, 2020, 4:04pm UTC](https://discourse.julialang.org/t/array-of-arrays-indexing-problem/50145/5 "2020-11-14T16:04:59Z")

</div>

I think you are confusing a `Vector` of `Vector`s with a matrix (i.e., `Array{T, 2}`). You can do:

```julia
julia> a = [1 2 3; 4 5 6; 7 8 9]
3×3 Array{Int64,2}:
 1 2 3
 4 5 6
 7 8 9

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

```

Note this is a single indexation passing `:` and `1` separated by a `,`, not two indexations executed one after the other. If you have a `Vector` of `Vectors` then the outer vector does not know about the inner ones (nor there is guarantee the inner vectors have the same size), so the solution by @pdeffebach is the way to go.

---

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [November 16, 2020, 12:11pm UTC](https://discourse.julialang.org/t/array-of-arrays-indexing-problem/50145/6 "2020-11-16T12:11:12Z")

</div>

> [@amael](#):
>
> Ok, `a[:]` is not simply an indexing operation. Thanks.

Well there is an allocation/copy with `a[:]`, but I don’t think that’s your issue here. With `a[:]`, you are accessing all the elements of `a`. You will get the same result with `view(a,:)[1]` where `view(a,:)` is “just indexing”.
