# Indexing of vector of arrays

**URL:** https://discourse.julialang.org/t/indexing-of-vector-of-arrays/111958
**Category:** New to Julia
**Created:** [March 22, 2024, 5:27am UTC](https://discourse.julialang.org/t/indexing-of-vector-of-arrays/111958 "2024-03-22T05:27:50Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![TongTongYee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tongtongyee/32/207244_2.png) [@TongTongYee](https://discourse.julialang.org/u/TongTongYee)
#### Post date: [March 22, 2024, 5:27am UTC](https://discourse.julialang.org/t/indexing-of-vector-of-arrays/111958/1 "2024-03-22T05:27:50Z")

</div>

Hi!

How do I index vector vector of arrays?  
Of course, I can easily index them in some cases. But I don’t know how to index them in specific cases.

For example,

```julia
A = Vector{Vector{Matrix{Int64}}}()
for i = 1:10
    push!(A, i*[[1 2 ; 3 4], [5 6; 7 8]])
end
julia> A
10-element Vector{Vector{Matrix{Int64}}}:
 [[1 2; 3 4], [5 6; 7 8]]
 [[2 4; 6 8], [10 12; 14 16]]
 [[3 6; 9 12], [15 18; 21 24]]
 [[4 8; 12 16], [20 24; 28 32]]
 ⋮
 [[8 16; 24 32], [40 48; 56 64]]
 [[9 18; 27 36], [45 54; 63 72]]
 [[10 20; 30 40], [50 60; 70 80]]

```

To index the following vector of matrix,

```julia
10-element Vector{Matrix{Int64}}:
 [1 2; 3 4]
 [2 4; 6 8]
 [3 6; 9 12]
 [4 8; 12 16]
 ⋮
 [8 16; 24 32]
 [9 18; 27 36]
 [10 20; 30 40]

```

I did try the following way.

```julia
A[:][1]
->
2-element Vector{Matrix{Int64}}:
 [1 2; 3 4]
 [5 6; 7 8]

A[:,1]
->
10-element Vector{Vector{Matrix{Int64}}}:
 [[1 2; 3 4], [5 6; 7 8]]
 [[2 4; 6 8], [10 12; 14 16]]
 [[3 6; 9 12], [15 18; 21 24]]
 [[4 8; 12 16], [20 24; 28 32]]
 ⋮
 [[8 16; 24 32], [40 48; 56 64]]
 [[9 18; 27 36], [45 54; 63 72]]
 [[10 20; 30 40], [50 60; 70 80]]

reduce(hcat,A)[1,:]
->
10-element Vector{Matrix{Int64}}:
 [1 2; 3 4]
 [2 4; 6 8]
 [3 6; 9 12]
 [4 8; 12 16]
 ⋮
 [8 16; 24 32]
 [9 18; 27 36]
 [10 20; 30 40]

```

reduce(hcat,A)[1,:] is successful. But if the object is vector of vector of vector of … of vector, then this way is maybe too complicate to index the object.

Is there an easy way to index such a object?

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [March 22, 2024, 9:26am UTC](https://discourse.julialang.org/t/indexing-of-vector-of-arrays/111958/2 "2024-03-22T09:26:51Z")

</div>

Try first.(A)

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [March 22, 2024, 11:35am UTC](https://discourse.julialang.org/t/indexing-of-vector-of-arrays/111958/3 "2024-03-22T11:35:22Z")

</div>

> [@rocco\_sprmnt21](#):
>
> Try first.(A)

Or `getindex.(A, 1)`, or `[a[1] for a in A]`, or …

> [@TongTongYee](#):
>
> I did try the following way. `A[:][1]`

You shouldn’t try syntax at random — try to understand how the language works. This expression first does `A[:]`, which makes a _copy_ of the whole 1d array `A`, and then takes the first element `[1]` of this copy (which is the same as the first element of `A`, i.e. a 2-element array of two 2x2 matrices).

> [@TongTongYee](#):
>
> `reduce(hcat,A)[1,:]` is successful

`reduce(hcat,A)` copies the whole array into a completely different data structure (see also the `stack` function). If you are doing something like this over and over, you should copy into another data structure (e.g. a 3d array) once.

An advantage of using a 3d or 4d array for something like this (as opposed to an array of 2d arrays, or an array of arrays of 2d arrays) is that then slices like `A[:, : , 1]` can [use views](https://docs.julialang.org/en/v1/base/arrays/#Views-(SubArrays-and-other-view-types)) (by adding the `@views` macro) rather than making copies of the data into new arrays.

Alternatively, if you have a 1d array of 2x2 matrices, and you know ahead of time that all your matrices will be 2x2 (e.g. you are doing 2d graphics), you should consider using StaticArrays.jl for the 2x2 matrices (e.g. a `Vector{SMatrix{2,2,Int,4}}`) which tends to be much more efficient than `Matrix` for small fixed sizes (and arrays thereof). (Or for an array of pairs of 2x2 matrices as in your example above, use an array of tuples of `SMatrix`.)

But without knowing what you want to _do_ with your arrays (and whether you care about performance), it’s hard to give more advice about data structures.
