# Preallocating array of matrices

**URL:** <https://discourse.julialang.org/t/preallocating-array-of-matrices/38665>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [May 3, 2020, 11:46am UTC](https://discourse.julialang.org/t/preallocating-array-of-matrices/38665 "2020-05-03T11:46:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![martincornejo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martincornejo/32/13436_2.png) [@martincornejo](https://discourse.julialang.org/u/martincornejo)\
**Post date:** [May 3, 2020, 11:46am UTC](https://discourse.julialang.org/t/preallocating-array-of-matrices/38665/1 "2020-05-03T11:46:14Z")

</div>

To preallocate an array of matrices we can write the following:

```julia
A = Array{Matrix{Float64}}(undef, n) # n being the number of array elements (matrices)

```

If we knew that each of the matrices in A has dimensions of **3 x 3** , would declaring that in the preallocation bring any performance advantages? If so, how could we write that?

---

<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:** [May 3, 2020, 12:03pm UTC](https://discourse.julialang.org/t/preallocating-array-of-matrices/38665/2 "2020-05-03T12:03:26Z")

</div>

It’s the same like in  
[https://discourse.julialang.org/t/how-to-create-an-array-of-preallocated-mutable-structs](https://discourse.julialang.org/t/how-to-create-an-array-of-preallocated-mutable-structs)  
/18482

So:

```julia
julia> n=10
10

julia> a = [Array{Float64,2}(undef,3,3) for i in 1:n]
10-element Array{Array{Float64,2},1}:
 [8.00472116e-316 1.613449943e-315 1.04815513e-315; 1.866005866e-315 1.85975376e-315 8.0408006e-316; 1.866006024e-315 1.61345105e-315 8.00472116e-316]
 [8.0055939e-316 1.613454053e-315 1.61345453e-315; 8.0055939e-316 1.61345421e-315 1.613454686e-315; 1.166857286e-315 1.61345437e-315 1.748721164e-315]
 [1.166857286e-315 8.00472116e-316 1.748721875e-315; 8.00472116e-316 1.61345453e-315 0.0; 1.61345421e-315 1.613454686e-315 0.0]
 [1.866006024e-315 1.61345105e-315 8.00472116e-316; 1.613449943e-315 1.04815513e-315 0.0; 1.85975376e-315 8.0408006e-316 0.0]
 [1.5e-323 5.0e-324 0.0; 5.0e-324 5.0e-324 0.0; 5.0e-324 5.0e-324 0.0]
 [8.0055939e-316 6.9226573e-316 6.9226668e-316; 8.0055939e-316 6.9226605e-316 6.92266997e-316; 6.92265416e-316 6.92266364e-316 6.92267313e-316]
 [1.166857286e-315 0.0 0.0; 1.613454686e-315 0.0 0.0; 1.74872662e-315 0.0 0.0]
 [1.866006024e-315 0.0 0.0; 8.0408006e-316 0.0 0.0; 8.00472116e-316 0.0 0.0]
 [5.0e-324 0.0 0.0; 5.0e-324 0.0 0.0; 0.0 0.0 0.0]
 [6.92265416e-316 1.748716974e-315 1.748717053e-315; 8.0055939e-316 8.0055939e-316 6.9226573e-316; 8.0055939e-316 8.0055939e-316 6.9226605e-316]

```

Note: I have written `Matrix{Float64}` as `Array{Float64,2}`

Depending on what you are doing it could bring some performance advantages.

---

<div class="post-metadata">

**Author:** ![colintbowers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colintbowers/32/8033_2.png) [@colintbowers](https://discourse.julialang.org/u/colintbowers)\
**Post date:** [May 4, 2020, 11:53pm UTC](https://discourse.julialang.org/t/preallocating-array-of-matrices/38665/3 "2020-05-04T23:53:24Z")

</div>

If you know for sure that every matrix is 3x3, then you should probably be using [StaticArrays](https://github.com/JuliaArrays/StaticArrays.jl). Depending on your application, the performance benefits can be huge.

---

<div class="post-metadata">

**Author:** ![jangevaare](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jangevaare/32/14430_2.png) [@jangevaare](https://discourse.julialang.org/u/jangevaare)\
**Post date:** [May 5, 2020, 3:12am UTC](https://discourse.julialang.org/t/preallocating-array-of-matrices/38665/4 "2020-05-05T03:12:52Z")

</div>

Higher dimensional arrays are an option here:

`Array{Float64, 3}(undef,3,3,100)`

---

<div class="post-metadata">

**Author:** ![martincornejo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martincornejo/32/13436_2.png) [@martincornejo](https://discourse.julialang.org/u/martincornejo)\
**Post date:** [May 5, 2020, 10:12am UTC](https://discourse.julialang.org/t/preallocating-array-of-matrices/38665/5 "2020-05-05T10:12:27Z")

</div>

> [@colintbowers](#):
>
> If you know for sure that every matrix is 3x3, then you should probably be using [StaticArrays](https://github.com/JuliaArrays/StaticArrays.jl). Depending on your application, the performance benefits can be huge.

Very interesting, I’ll check it out

> [@jangevaare](#):
>
> Higher dimensional arrays are an option here:
> 
> `Array{Float64, 3}(undef,3,3,100)`

Also a possibility, but in my case an array of matrices is much more convenient
