Allocating a space for a 1d array of matrices

memory-allocation
#1

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

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

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

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

obviously does not work.

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

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

2 Likes
#3

Many thanks.