# Array indexing

**URL:** <https://discourse.julialang.org/t/array-indexing/64095>\
**Category:** Internals & Design\
**Tags:** array, arrayofarrays\
**Created:** [July 5, 2021, 6:08pm UTC](https://discourse.julialang.org/t/array-indexing/64095 "2021-07-05T18:08:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcell\_Havlik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marcell_havlik/32/37424_2.png) [@Marcell\_Havlik](https://discourse.julialang.org/u/Marcell_Havlik)\
**Post date:** [July 5, 2021, 6:08pm UTC](https://discourse.julialang.org/t/array-indexing/64095/1 "2021-07-05T18:08:41Z")

</div>

Hey Julianners,

Are there a way to ha a syntax like this:

```julia
arrofarr = [randn(2,4) for _ in 1:10]
arrofarr[7:end][1:2,3:end] # would select a [4] x [2,2] dim array.

```

Is this possible? It would be really clean and it could simplify some operation at many place. Also it sounds very intuitive (but of course inbound checks are necessary)

For me I use a function like this:

```julia
access_elems(arrs, i) = collect(map(x->x[i], arrs)) # collect is only for timing
access_elems(arrofarr[7:end], [1:2,3:size(arrofarr[1],2)])

```

which isn’t so nice.

Does anyone know if there is a chance this could be improved later?

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [July 5, 2021, 6:17pm UTC](https://discourse.julialang.org/t/array-indexing/64095/2 "2021-07-05T18:17:12Z")

</div>

You could use another comprehension for this:

```julia
[a[1:2, 3:end] for a in arrofarr[7:end] ]

```

---

<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:** [July 5, 2021, 6:51pm UTC](https://discourse.julialang.org/t/array-indexing/64095/3 "2021-07-05T18:51:08Z")

</div>

You could also broadcast a `getindex` call, although it’s not as pretty as a comprehension:

```julia
 getindex.(arrofarr[7:end], Ref(1:2), Ref(3:4))

```

`VectorOfArray` from [RecursiveArrayTools](https://github.com/SciML/RecursiveArrayTools.jl) may be useful, although it’ll return a 3D array with the vector dimension last rather than a vector of 2D arrays:

```julia
julia> using RecursiveArrayTools

julia> arrarr = VectorOfArray([randn(2,4) for _ in 1:10]);

julia> size(arrarr)
(2, 4, 10)

julia> arrarr[1:2, 3:end, 7:end]
2×2×4 Array{Float64, 3}:
[:, :, 1] =
 -1.15581 0.319321
  1.64091 0.576849

[:, :, 2] =
  0.429193 1.08883
 -1.79132 0.3654

[:, :, 3] =
 -0.326053 1.34863
  0.936408 -0.273089

[:, :, 4] =
  1.05441 1.38047
 -0.876187 -0.438934

```

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [July 5, 2021, 6:58pm UTC](https://discourse.julialang.org/t/array-indexing/64095/4 "2021-07-05T18:58:11Z")

</div>

Nice. I actually failed to find the right syntax with getindex and gave up in favour for the comprehension 😉

---

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [July 5, 2021, 7:03pm UTC](https://discourse.julialang.org/t/array-indexing/64095/5 "2021-07-05T19:03:32Z")

</div>

If you want to use this kind of indexing, presumably the inner arrays are all of the same size, and you probably should represent this data as a multi-dimensional array rather than vector-of-arrays to begin with:

```julia
julia> arr = reduce((a,b)->cat(a,b;dims=3), randn(2,4) for _ in 1:10);

julia> arr[1:2,3:end,7:end]
2×2×4 Array{Float64, 3}:
[:, :, 1] =
 0.498871 1.33621
 1.22567 -0.367682

[:, :, 2] =
 -0.663532 0.911144
  0.715724 -0.256871

[:, :, 3] =
 -0.923242 -0.768945
 -0.77289 0.189791

[:, :, 4] =
  0.44265 -0.381229
 -0.996726 -0.991616

```

---

<div class="post-metadata">

**Author:** ![Marcell\_Havlik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marcell_havlik/32/37424_2.png) [@Marcell\_Havlik](https://discourse.julialang.org/u/Marcell_Havlik)\
**Post date:** [July 5, 2021, 8:04pm UTC](https://discourse.julialang.org/t/array-indexing/64095/6 "2021-07-05T20:04:11Z")

</div>

Thank you for the lot of tip.

For me I can’t really switch to 3D in spite of the facts that I have the same dims. I have batch of 2D matrices in my case and I have to extend it a lot of time.

Thank you for showing the way, how to do the getindex broadcast version. 🙂

So don’t we want to have some support on this `array[range1][range2, range3...]` It is just beautiful and something that so intuitive, everyone start the syntax with that.

I don’t want to take the emphasis from this topic but are the any plan to support syntactic like:

```julia
array[3:] # instead of array[3:end]
array[3:-2] # instead of array[3:end-2]

```

It is beautiful too and pretty intuitive. 🙂

---

<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:** [July 5, 2021, 8:41pm UTC](https://discourse.julialang.org/t/array-indexing/64095/7 "2021-07-05T20:41:58Z")

</div>

`VectorOfArray` isn’t actually stored as a contiguous 3D array in memory, it just has a specialized `getindex` method that lets you treat it like a 3D array. You can still append to it like `arrofarr` with `push!(arrarr.u, rand(2, 4))`. [`ElasticArrays.jl`](https://github.com/JuliaArrays/ElasticArrays.jl) serves a similar purpose in that it allows you to append to the last dimension of an N-dimensional array, but it’s stored contiguously in memory, so you can’t insert/delete elements in the middle like you can with a `VectorOfArray`.

The `array[range1][range2, range3]` syntax is unworkable because of Julia’s order of operations: `array[range1]` is processed first, and that returns a sub-array of the same shape as the original array (in this case, a `Vector`):

```julia
julia> tmp = arrofarr[1:2]
2-element Vector{Matrix{Float64}}:
 [-1.3003492397624834 0.9935985952081343 2.2835928481588357 -2.0331284677618404; 1.0502180694922136 0.5962378647963208 1.332261466006276 0.9387068354558264]
 [-1.1424632759265543 -0.09666017943952224 0.5520569751178473 -0.27689019750702204; 1.602555685315125 2.588662242106899 -0.462705003748125 -0.735344944870217]

```

You can’t index into a vector as if it’s a 2D array for obvious reasons; you need to broadcast the `getindex` call for each matrix element of the vector.

```julia
julia> tmp[2, 4]
ERROR: BoundsError: attempt to access 2-element Vector{Matrix{Float64}} at index [2, 4]

julia> tmp[1][2, 4]
0.9387068354558264

julia> getindex.(tmp, 2, 4)
2-element Vector{Float64}:
  0.9387068354558264
 -0.735344944870217

```

There’s a longstanding [active proposal](https://github.com/JuliaLang/julia/issues/19169) to allow index broadcasting with `arrofarr[7:end].[1:2, 3:end]`, and while everybody agrees that it’s nice syntax, there are some corner cases that’ve prevented a decision from being made.

---

<div class="post-metadata">

**Author:** ![Marcell\_Havlik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marcell_havlik/32/37424_2.png) [@Marcell\_Havlik](https://discourse.julialang.org/u/Marcell_Havlik)\
**Post date:** [July 5, 2021, 10:43pm UTC](https://discourse.julialang.org/t/array-indexing/64095/8 "2021-07-05T22:43:14Z")

</div>

That is exactly what I need! How can I send a sign that is it really good idea? 🤓
