# Pre-allocation for matrices / vectors of matrices of different length

**URL:** <https://discourse.julialang.org/t/pre-allocation-for-matrices-vectors-of-matrices-of-different-length/97071>\
**Category:** General Usage\
**Created:** [April 4, 2023, 4:20pm UTC](https://discourse.julialang.org/t/pre-allocation-for-matrices-vectors-of-matrices-of-different-length/97071 "2023-04-04T16:20:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Antomek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antomek/32/9406_2.png) [@Antomek](https://discourse.julialang.org/u/Antomek)\
**Post date:** [April 4, 2023, 4:20pm UTC](https://discourse.julialang.org/t/pre-allocation-for-matrices-vectors-of-matrices-of-different-length/97071/1 "2023-04-04T16:20:28Z")

</div>

Hi all,

I am doing a bunch of simulations, and I want to save the outcome of these `N` simulations  
My parameters for each simulation is a `p ` of type `Vector{Matrix{Float32}}`, and there are `M` vectors `X, Y` that I’d like to store for each `p`.  
The dimensions of `p`, `X`, and `Y` are known a priori.

Currently I’m doing something along the lines of

```julia
data = Dict()
X_dim = ... # lenght of X
Y_dim = ... # length of Y
for i in 1:N
    p = generate_params()
    Xs = Matrix{Float32}(undef, M, X_dim)
    Ys = Matrix{Float32}(undef, M, Y_dim)
    for j in 1:M
        X = rand(X_dim)
        Y = simulation(X, p)
        Xs[j, :] .= X
        Ys[j, :] .= Y
    end
    merge!(dict, i => (p = p, Xs = Xs, Ys = Ys))
end

```

to store the data.  
But (unsurprisingly) this leads to a lot of garbage collection time, and I’m wondering if there is a good way of preallocating memory to speed things up.  
I suspect there must be, as all dimensions are known, but I can’t figure out an elegant way to do it that still keeps track of all the data easily!

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [April 4, 2023, 4:32pm UTC](https://discourse.julialang.org/t/pre-allocation-for-matrices-vectors-of-matrices-of-different-length/97071/2 "2023-04-04T16:32:50Z")

</div>

Do you really need a dictionary if all you are doing is indexing simulations with `i in 1:N`? Why not a vector of matrices?

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [April 4, 2023, 4:37pm UTC](https://discourse.julialang.org/t/pre-allocation-for-matrices-vectors-of-matrices-of-different-length/97071/3 "2023-04-04T16:37:47Z")

</div>

It seems to me that the only allocations that can be avoided are `X` and `Y`.

You need to allocate `N` instances of `p`, `Xs`, and `Ys` b/c you store those.  
Assigning them to the `Dict` does not allocate. Though I don’t know how `merge!` operates. `d[i] = (p = p, Xs = Xs, Ys = Ys)` would seem easier anyway.

To avoid allocating `X` and `Y`, in your loop:

```julia
@views for j = 1 : M
  Xs[j,:] .= rand(X_dim)
  Ys[j,:] .= simulation(Xs[j,:], p)
  # or simulation!(Ys[j,:], Xs[j,:], p)
end

```

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [April 4, 2023, 4:43pm UTC](https://discourse.julialang.org/t/pre-allocation-for-matrices-vectors-of-matrices-of-different-length/97071/4 "2023-04-04T16:43:29Z")

</div>

You could even do `rand!(Xs[j,:])`
