Preallocating array of matrices

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

So:

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.

3 Likes