# Correctly initializing collection of matrices

**URL:** <https://discourse.julialang.org/t/correctly-initializing-collection-of-matrices/62116>\
**Category:** New to Julia\
**Tags:** arrays, collection, matrices\
**Created:** [May 31, 2021, 8:37am UTC](https://discourse.julialang.org/t/correctly-initializing-collection-of-matrices/62116 "2021-05-31T08:37:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nandoconde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nandoconde/32/19497_2.png) [@nandoconde](https://discourse.julialang.org/u/nandoconde)\
**Post date:** [May 31, 2021, 8:37am UTC](https://discourse.julialang.org/t/correctly-initializing-collection-of-matrices/62116/1 "2021-05-31T08:37:18Z")

</div>

Hello!

I am following suggestions from another post and, instead of having a 3-D array, I am refactoring my code to use a `Vector` of `Matrix` so I can use broadcasting on the matrices.

Is there a way to initialize the whole structure so I can fill its values later?

I could do this for 3D case:

```julia
S = Array{ComplexF64,3}(undef,4,4,1000)

```

But I can’t find a way to do it for the new code. Is it possible? Is there lack of performance if I don’t initialize it? The advantage is that I guess I could always `push` new matrices to the array, but that doesn’t sound as well-performing to me ☹

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [May 31, 2021, 9:04am UTC](https://discourse.julialang.org/t/correctly-initializing-collection-of-matrices/62116/2 "2021-05-31T09:04:03Z")

</div>

You can use a list comprehension if you know the number of matrices in your vector. For example,

`x = [Array{Complex,3}(undef,4,4,1000) for _ in 1:5]`

With this approach you can add values to the matrices later. As you suggested, if you do not know the size, you define an empty vector and push arrays into it as follows:

```julia
x = Vector{Array{Complex,3}}()
push!(x, rand(4, 4, 4))

```

With more context about your problem, we might be able to provide more specific advice.

---

<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 31, 2021, 9:47am UTC](https://discourse.julialang.org/t/correctly-initializing-collection-of-matrices/62116/3 "2021-05-31T09:47:05Z")

</div>

For example (vector of undefed 3x3 matrices):

```julia
v = [Matrix{ComplexF64}(undef,3,3) for _ in 1:5]

```

or (inititalized with 0+0im):

```julia
v = collect( (reshape(zeros(ComplexF64,9),3,3) for _ in 1:5) )

```

The first one (comprehension) is twice as fast as the second one (generator).

---

<div class="post-metadata">

**Author:** ![nandoconde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nandoconde/32/19497_2.png) [@nandoconde](https://discourse.julialang.org/u/nandoconde)\
**Post date:** [May 31, 2021, 9:49am UTC](https://discourse.julialang.org/t/correctly-initializing-collection-of-matrices/62116/4 "2021-05-31T09:49:29Z")

</div>

Thanks for the answers!!

My inner matrices are 4x4. Does using `MMatrix` instead of `Matrix` pay off?

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [May 31, 2021, 10:13am UTC](https://discourse.julialang.org/t/correctly-initializing-collection-of-matrices/62116/5 "2021-05-31T10:13:15Z")

</div>

Sorry. I am not familiar with `MMatrix`. What is 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 31, 2021, 10:13am UTC](https://discourse.julialang.org/t/correctly-initializing-collection-of-matrices/62116/6 "2021-05-31T10:13:55Z")

</div>

For `StaticArrays` the performance is somehow equal and in between the non-static examples above:

```julia
julia> using StaticArrays

julia> @btime v = MVector{5}( [MMatrix{4,4}(zeros(ComplexF64,4*4)) for _ in 1:5] );
  262.812 ns (12 allocations: 3.14 KiB)

julia> @btime v = MVector{5}( collect( (MMatrix{4,4}(zeros(ComplexF64,4*4)) for _ in 1:5) ) );
  278.275 ns (12 allocations: 3.14 KiB)

```

It may pay off, depending on your complete code.
