# How To Create a 2D Array of Arrays?

**URL:** <https://discourse.julialang.org/t/how-to-create-a-2d-array-of-arrays/135178>\
**Category:** New to Julia\
**Tags:** array, arrays, matrix\
**Created:** [January 21, 2026, 6:54am UTC](https://discourse.julialang.org/t/how-to-create-a-2d-array-of-arrays/135178 "2026-01-21T06:54:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Grass](https://avatars.discourse-cdn.com/v4/letter/g/96bed5/32.png) [@Grass](https://discourse.julialang.org/u/Grass)\
**Post date:** [January 21, 2026, 6:54am UTC](https://discourse.julialang.org/t/how-to-create-a-2d-array-of-arrays/135178/1 "2026-01-21T06:54:33Z")

</div>

Is it possible to create a 2D array of (2D) arrays in Julia? If so, how can I do it?

I scoured the internet but I did not manage to find out whether this is possible or not. I only managed to find information on creating 1D arrays of arrays. E.g.

```julia
A = Array[[1, 0], [0, 1] ]

```

creates a 2-element vector whose elements are 2-element vectors of type `Int`.

---

<div class="post-metadata">

**Author:** ![karei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karei/32/214809_2.png) [@karei](https://discourse.julialang.org/u/karei)\
**Post date:** [January 21, 2026, 7:33am UTC](https://discourse.julialang.org/t/how-to-create-a-2d-array-of-arrays/135178/2 "2026-01-21T07:33:24Z")

</div>

Yes, you can do it!

Suppose you want to create a 2×2 matrix, where each element of the matrix is a 1×1 submatrix.

```julia
> a=[1;;]
1×1 Matrix{Int64}:
 1

> b=[a for i=1:2,j=1:2]
2×2 Matrix{Matrix{Int64}}:
 [1;;] [1;;]
 [1;;] [1;;]
```

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [January 21, 2026, 7:48am UTC](https://discourse.julialang.org/t/how-to-create-a-2d-array-of-arrays/135178/3 "2026-01-21T07:48:13Z")

</div>

Note that with the construction given above, all the elements of the big array will point to the same location in memory, so modifying the elements of a will modify each entry of b

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [January 21, 2026, 7:50am UTC](https://discourse.julialang.org/t/how-to-create-a-2d-array-of-arrays/135178/4 "2026-01-21T07:50:08Z")

</div>

You can use [comprehensions](https://docs.julialang.org/en/v1/manual/arrays/#man-comprehensions) to create arrays, for example

```julia
julia> [i+j+1 for i=1:2, j=1:2]
2×2 Matrix{Int64}:
 3 4
 4 5

```

which creates a matrix of integers. We can either nest these, or find other ways to create a matrix per entry, for example we could create the same with constant entries

```julia-auto
julia> [(i+j+1)*ones(2,2) for i=1:2, j=1:2]
2×2 Matrix{Matrix{Float64}}:
 [3.0 3.0; 3.0 3.0] [4.0 4.0; 4.0 4.0]
 [4.0 4.0; 4.0 4.0] [5.0 5.0; 5.0 5.0]

```

Then every entry is a 2x2 matrix, where each 2x2 matrix has the value from before.  
You can also see in the type `Matrix{Matrix{Float64}}` that this is a matrix of matrices.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [January 21, 2026, 8:02am UTC](https://discourse.julialang.org/t/how-to-create-a-2d-array-of-arrays/135178/5 "2026-01-21T08:02:40Z")

</div>

_Creating_ a matrix of matrices is not a problem and can be done in many ways. In addition to other answers,

```julia
julia> Array{Matrix{Int}}(undef, 2, 3)
2×3 Matrix{Matrix{Int64}}:
 #undef #undef #undef
 #undef #undef #undef

```

gives you one that you can later fill in with the element matrices.

If you want to write it as a _literal_ it becomes a little trickier since Julia’s matrix literals perform concatenation. E.g.

```julia-auto
julia> [[1 1; 1 1] [2 2; 2 2]; [3 3; 3 3] [4 4; 4 4]]
4×4 Matrix{Int64}:
 1 1 2 2
 1 1 2 2
 3 3 4 4
 3 3 4 4

```

You can work around this by wrapping your small matrices in intermediate arrays

```julia-auto
julia> [[[1 1; 1 1]] [[2 2; 2 2]]; [[3 3; 3 3]] [[4 4; 4 4]]]
2×2 Matrix{Matrix{Int64}}:
 [1 1; 1 1] [2 2; 2 2]
 [3 3; 3 3] [4 4; 4 4]

```

So not super elegant, but possible.

---

<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:** [January 21, 2026, 9:50am UTC](https://discourse.julialang.org/t/how-to-create-a-2d-array-of-arrays/135178/6 "2026-01-21T09:50:52Z")

</div>

> [@Grass](#):
>
> ```julia-auto
> A = Array[[1, 0], [0, 1] ]
> 
> ```
> 
> creates a 2-element vector whose elements are 2-element vectors of type `Int`.

Note that this is actually incorrect:

```julia-auto
julia> A = Array[[1, 0], [0, 1] ]
2-element Vector{Array}:
 [1, 0]
 [0, 1]

```

It creates a vector of `Array`, where `Array` has not specification of either dimensionality or element type. To get `Vector{Vector{Int}}`, remove the type specification at the start:

```julia-auto
julia> [[1, 0], [0, 1] ]
2-element Vector{Vector{Int64}}:
 [1, 0]
 [0, 1]

```

What happens is that if you write `SomeType[a, b, c]` then you will get a `Vector{SomeType}`.
