# Allocating a space for a 1d array of matrices

**URL:** <https://discourse.julialang.org/t/allocating-a-space-for-a-1d-array-of-matrices/22342>\
**Category:** Performance\
**Tags:** memory-allocation\
**Created:** [March 26, 2019, 10:06am UTC](https://discourse.julialang.org/t/allocating-a-space-for-a-1d-array-of-matrices/22342 "2019-03-26T10:06:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)\
**Post date:** [March 26, 2019, 10:06am UTC](https://discourse.julialang.org/t/allocating-a-space-for-a-1d-array-of-matrices/22342/1 "2019-03-26T10:06:56Z")

</div>

What is the right way to allocate space for a 1d array of matrices?

In particular, I know that that in my algorithm I will have to determine a sequence matrices, each of size `n` x `m` with Float64 entries. The length `N` of the sequence is known apriori. I will keep determining one matrix after another (in a loop), referencing them as `A[k]`. It seems wise to allocate the space for such data. If if write something like

```julia
A = Array{Matrix{Float64}}(undef,10)

```

then Julia has no way to determine the size of individual matrices. Writing something like

```julia
A = Array{Matrix{Float64}(undef,2,2)}(10)

```

obviously does not work.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 26, 2019, 10:14am UTC](https://discourse.julialang.org/t/allocating-a-space-for-a-1d-array-of-matrices/22342/2 "2019-03-26T10:14:43Z")

</div>

```julia
[Matrix{Float64}(undef, 2, 2) for _ in 1:10]

```

If the matrices are small, you may want to consider `StaticArrays.MArray`.

---

<div class="post-metadata">

**Author:** ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)\
**Post date:** [March 26, 2019, 10:18am UTC](https://discourse.julialang.org/t/allocating-a-space-for-a-1d-array-of-matrices/22342/3 "2019-03-26T10:18:22Z")

</div>

> [@Tamas\_Papp](#):
>
> [Matrix{Float64}(undef, 2, 2) for \_ in 1:10]

Many thanks.
