# How to extract components in an array of arrays

**URL:** https://discourse.julialang.org/t/how-to-extract-components-in-an-array-of-arrays/79515
**Category:** General Usage
**Tags:** question, arrays
**Created:** [April 15, 2022, 4:55am UTC](https://discourse.julialang.org/t/how-to-extract-components-in-an-array-of-arrays/79515 "2022-04-15T04:55:44Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![BVPs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bvps/32/2190_2.png) [@BVPs](https://discourse.julialang.org/u/BVPs)
#### Post date: [April 15, 2022, 4:55am UTC](https://discourse.julialang.org/t/how-to-extract-components-in-an-array-of-arrays/79515/1 "2022-04-15T04:55:44Z")

</div>

Suppose I have an array of arrays, say a 2 x 2 matrix whose entries are 1 x 2 vectors, e.g.,

```julia
julia> A
2×2 Matrix{Matrix{Int64}}:
 [1 2] [3 4]
 [5 6] [7 8]

```

I am wondering, without using `for` loops, how to extract a 2 x 2 matrix consisting of the 1st entries of this matrix of matrices, i.e.,

```julia
2x2 Matrix{Int64}
1 3
5 7

```

Also, a matrix of its 2nd entries, i.e.,

```julia
2x2 Matrix{Int64}
2 4
6 8

```

Is there a nice and quick way to do this? (Of course, what I really want is to do the same for large matrices of matrices or vectors, but this 2x2 example sufficiently describe my intention.)  
Thanks a lot in advance!

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [April 15, 2022, 5:14am UTC](https://discourse.julialang.org/t/how-to-extract-components-in-an-array-of-arrays/79515/2 "2022-04-15T05:14:26Z")

</div>

You can broadcast `getindex.(A,1)` which gets index 1 for each element of the array.

---

<div class="post-metadata">

### Author: ![BVPs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bvps/32/2190_2.png) [@BVPs](https://discourse.julialang.org/u/BVPs)
#### Post date: [April 15, 2022, 5:34am UTC](https://discourse.julialang.org/t/how-to-extract-components-in-an-array-of-arrays/79515/3 "2022-04-15T05:34:00Z")

</div>

Thank you so much! This is quite helpful!

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [April 15, 2022, 6:13am UTC](https://discourse.julialang.org/t/how-to-extract-components-in-an-array-of-arrays/79515/4 "2022-04-15T06:13:00Z")

</div>

Specifically for the first and last elements, you can use `first.(A)` and `last.(A)`.

BTW, there’s no such thing as a 1x2 vector. That is a matrix.

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [April 15, 2022, 6:13am UTC](https://discourse.julialang.org/t/how-to-extract-components-in-an-array-of-arrays/79515/5 "2022-04-15T06:13:26Z")

</div>

Note that in Julia vocab, `[1 2]` is not a vector but a 1x2 matrix. If you don’t have a specific reason to store your data like this, using `[1, 2]` is more Julian.

---

<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: [April 15, 2022, 7:30am UTC](https://discourse.julialang.org/t/how-to-extract-components-in-an-array-of-arrays/79515/6 "2022-04-15T07:30:41Z")

</div>

what is (are they?) the syntax to use to build by hand a matrix like A that has matrices as elements?

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [April 15, 2022, 8:37am UTC](https://discourse.julialang.org/t/how-to-extract-components-in-an-array-of-arrays/79515/7 "2022-04-15T08:37:56Z")

</div>

> [@rocco\_sprmnt21](#):
>
> what is (are they?) the syntax to use to build by hand a matrix like A that has matrices as elements?

The following reproduces `A`:

```julia
A = [[[1 2]] [[3 4]]; [[5 6]] [[7 8]]]  

```

---

<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: [April 15, 2022, 9:21am UTC](https://discourse.julialang.org/t/how-to-extract-components-in-an-array-of-arrays/79515/8 "2022-04-15T09:21:37Z")

</div>

I have tried these ways, but one doesn’t work and I didn’t like the other as much

```julia
julia> [[1 2] [3 4]; [5 6] [7 8]]
2×4 Matrix{Int64}:
 1 2 3 4
 5 6 7 8

julia> reshape([[1 2], [5 6], [3 4], [7 8]],2,2)
2×2 Matrix{Matrix{Int64}}:
 [1 2] [3 4]
 [5 6] [7 8]

```

instead, this creates an array of tuples.  
Why in the case of vectors need to be wrapped inside another vector?

```julia
julia> [(1,2) (3,4); (5,6) (7,8)]
2×2 Matrix{Tuple{Int64, Int64}}:
 (1, 2) (3, 4)
 (5, 6) (7, 8)

```

anhoter way

```julia
julia> [[[1 2], [5 6]] [[3 4], [7 8]]]
2×2 Matrix{Matrix{Int64}}:
 [1 2] [3 4]
 [5 6] [7 8]

```

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [April 15, 2022, 10:54am UTC](https://discourse.julialang.org/t/how-to-extract-components-in-an-array-of-arrays/79515/9 "2022-04-15T10:54:34Z")

</div>

> [@rocco\_sprmnt21](#):
>
> Why in the case of vectors need to be wrapped inside another vector?

I think this follows from Julia’s rule that square braces surrounding elements separated by spaces are equivalent to horizontal concatenation:

```julia
[A B] == hcat(A,B)

```

---

<div class="post-metadata">

### Author: ![BVPs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bvps/32/2190_2.png) [@BVPs](https://discourse.julialang.org/u/BVPs)
#### Post date: [April 15, 2022, 4:14pm UTC](https://discourse.julialang.org/t/how-to-extract-components-in-an-array-of-arrays/79515/10 "2022-04-15T16:14:14Z")

</div>

Thanks, you are right. What I meant was row vectors of length 2, which are 1 x 2 matrices.

---

<div class="post-metadata">

### Author: ![BVPs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bvps/32/2190_2.png) [@BVPs](https://discourse.julialang.org/u/BVPs)
#### Post date: [April 15, 2022, 7:33pm UTC](https://discourse.julialang.org/t/how-to-extract-components-in-an-array-of-arrays/79515/11 "2022-04-15T19:33:33Z")

</div>

BTW, what I really wanted to do is to generate a vector field on a 2D plane, say, the unit square, and draw that vector field using `quiver` plot in Plots.jl. So, here is an example on a very small grid.

```julia
julia> using LazyGrids # this allows to generate meshes like in MATLAB
julia> X, Y = ndgrid((collect(0:1), collect(0:1));
julia> X
2×2 LazyGrids.GridAV{Int64, 1, 2}:
 0 0
 1 1
julia> Y
2×2 LazyGrids.GridAV{Int64, 2, 2}:
 0 1
 0 1
julia> f(x,y) = [0.0 1.0; 1.0 0.0] * [x; y]; # Just a simple example of generating a vector at (x,y).
julia> A = f.(X,Y) # pointwise evaluation of f on the mesh
2×2 Matrix{Vector{Float64}}:
 [0.0, 0.0] [1.0, 0.0]
 [0.0, 1.0] [1.0, 1.0]

```

Then, in order to use `quiver` plot, I need to extract the first components and the second components:

```julia
quiver(X[:], Y[:], quiver=(getindex.(A,1)[:], getindex.(A,2)[:]))

```

Perhaps, there is a better way to generate a vector field, which can easily supply to the `quiver` plot…

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [April 16, 2022, 12:57pm UTC](https://discourse.julialang.org/t/how-to-extract-components-in-an-array-of-arrays/79515/12 "2022-04-16T12:57:07Z")

</div>

It should be more convenient to work with vectors as they are, instead of separate components:

```julia
julia> using RectiGrids
julia> using StaticArrays

# array of SVectors, instead of two separate arrays:
julia> XY = grid(SVector, 0:1, 0:1) 
2-dimensional KeyedArray(...) with keys:
↓ 2-element UnitRange{Int64}
→ 2-element UnitRange{Int64}
And data, 2×2 RectiGrids.RectiGridArr{Base.OneTo(2), SVector{2, Int64}, 2, Tuple{UnitRange{Int64}, UnitRange{Int64}}}:
      (0) (1)
 (0) [0, 0] [0, 1]
 (1) [1, 0] [1, 1]

# f() takes the whole svector, not two separate components:
julia> f(xy) = [0. 1; 1 0] * xy
f (generic function with 1 method)

julia> A = f.(XY)
2-dimensional KeyedArray(...) with keys:
↓ 2-element UnitRange{Int64}
→ 2-element UnitRange{Int64}
And data, 2×2 Matrix{Vector{Float64}}:
      (0) (1)
 (0) [0.0, 0.0] [1.0, 0.0]
 (1) [0.0, 1.0] [1.0, 1.0]

```

Only in the end, if you actually need to split them into components:

```julia
julia> using SplitApplyCombine

julia> invert(XY)
2-element Vector{Matrix{Int64}}:
 [0 0; 1 1]
 [0 1; 0 1]

julia> invert(A)
2-element Vector{Matrix{Float64}}:
 [0.0 1.0; 0.0 1.0]
 [0.0 0.0; 1.0 1.0]

```
