# What's the proper way to create and initialize a vector of vectors of matrices and vectors?

**URL:** https://discourse.julialang.org/t/whats-the-proper-way-to-create-and-initialize-a-vector-of-vectors-of-matrices-and-vectors/40515
**Category:** General Usage
**Created:** [May 31, 2020, 7:25pm UTC](https://discourse.julialang.org/t/whats-the-proper-way-to-create-and-initialize-a-vector-of-vectors-of-matrices-and-vectors/40515 "2020-05-31T19:25:05Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![kai](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@kai](https://discourse.julialang.org/u/kai)
#### Post date: [May 31, 2020, 7:25pm UTC](https://discourse.julialang.org/t/whats-the-proper-way-to-create-and-initialize-a-vector-of-vectors-of-matrices-and-vectors/40515/1 "2020-05-31T19:25:05Z")

</div>

Hi, I’m trying to create and initialize two objects: `x` will be a vector of vector of vectors, and `y` will be a vector of vector of matrices. I tried

```julia
x =Vector{Vector{Vector{Float64}}}(undef, 5)
y =Vector{Vector{Matrix{Float64}}}(undef, 5)

```

But ran into `ERROR: UndefRefError: access to undefined reference` when trying to fill in the content with `x[1][1] = ones(3)` or `y[1][1] = ones(3,3)`.

I wonder what is the proper way to do this?

---

<div class="post-metadata">

### Author: ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)
#### Post date: [May 31, 2020, 8:14pm UTC](https://discourse.julialang.org/t/whats-the-proper-way-to-create-and-initialize-a-vector-of-vectors-of-matrices-and-vectors/40515/2 "2020-05-31T20:14:21Z")

</div>

This is because the elements of `x` and `y` are `undef` and you have to define them first.

```julia
julia> x = Vector{Vector{Vector{Float64}}}(undef,5);

julia> x[1]
ERROR: UndefRefError: access to undefined reference
Stacktrace:
 [1] getindex(::Array{Array{Array{Float64,1},1},1}, ::Int64) at .\array.jl:788
 [2] top-level scope at REPL[2]:1

julia> x[1] = Vector{Vector{Float64}}(undef,2)
2-element Array{Array{Float64,1},1}:
 #undef
 #undef

julia> x[1][1] = ones(3)
3-element Array{Float64,1}:
 1.0
 1.0
 1.0

```

The data structure you are constructing is fairly complicated: what are you trying to achieve?

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [May 31, 2020, 11:10pm UTC](https://discourse.julialang.org/t/whats-the-proper-way-to-create-and-initialize-a-vector-of-vectors-of-matrices-and-vectors/40515/3 "2020-05-31T23:10:15Z")

</div>

I think the types you are using are a little worrying (and this is coming from someone that uses `Tuple`s and `Vector`s for everything). The easiest way to initialize them are probably list comprehensions, for example:

```julia
julia> [[Vector{Float64}(undef, 4) for _ = 1:5] for _ = 1:6]
5-element Array{Array{Array{Float64,1},1},1}:
...

```

This list comprehension gives a `Vector{Vector{Vector{Float64}}}`, it is a vector of six elements, each element is a vector of size five, and each of those five is a vector of four elements inside.

---

<div class="post-metadata">

### Author: ![kai](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@kai](https://discourse.julialang.org/u/kai)
#### Post date: [May 31, 2020, 11:32pm UTC](https://discourse.julialang.org/t/whats-the-proper-way-to-create-and-initialize-a-vector-of-vectors-of-matrices-and-vectors/40515/4 "2020-05-31T23:32:01Z")

</div>

Thanks for the solution!

This is for constructing a type that will be used for saving results from simulations. The simulation runs on different subsets (the first Vector), on each subset there are many repeats (the second vector), at each repeat I would get results as vectors and matrices (the third Vector, or Matrix).

I’ve thought about whether to use NameTuples, but from what I read, I don’t think there’s an advantage in using them. What’s your thought?

---

<div class="post-metadata">

### Author: ![kai](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@kai](https://discourse.julialang.org/u/kai)
#### Post date: [May 31, 2020, 11:35pm UTC](https://discourse.julialang.org/t/whats-the-proper-way-to-create-and-initialize-a-vector-of-vectors-of-matrices-and-vectors/40515/5 "2020-05-31T23:35:51Z")

</div>

Thanks for your solution too.

> I think the types you are using are a little worrying (and this is coming from someone that uses `Tuple` s and `Vector` s for everything).

Is there a different structure you would suggest? Like I explained in my response to @mzaffalon, I’ve thought about using NamedTuples, but thought that Vector just seems easier to implement.

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [June 1, 2020, 12:11am UTC](https://discourse.julialang.org/t/whats-the-proper-way-to-create-and-initialize-a-vector-of-vectors-of-matrices-and-vectors/40515/6 "2020-06-01T00:11:39Z")

</div>

If all the elements of some outer `Vector` will be `Vector`s of some shared and fixed size, I would suggest using an `Array{Float64, 3}` for your `x` (and a `Array{Float64, 4}` for your `y`).

If you need “sibling” `Vector`s to have different sizes (e.g., `x[1]` and `x[2]`, or `x[1][2]` and `x[1][3]`), then the things complicate, your solution can end up being the best one, but you could also define `struct` types for ‘Sample{T}’ and ‘Experiment{T}’ so you can shorten the type to `Experiment{T}` that has a field `configs :: Vector{Sample{T}}` or a `Dict{Symbol, Sample{T}}` inside (if you prefer to give names to the configs, instead of using numbers), and `Sample{T}` has a field `datapoints :: Vector{T}` (or maybe named `results`). It is up to you.

Probably what I find more strange is that you are defining and preallocating the whole structure before running the experiments and need to write/define the whole type in a line. The way I wrote Julia code, I can end up with a “Vector of Vector of Vector of Type T” but probably I will have some function that executes a `map` over some data, and the function that is applied also executes a `map` of a function that ends up returning a `Vector` so I end up with the same three-layered object but I never had to explicit its type, it is just the return of functions I call that allocate the vectors of the right types and sizes for me.

---

<div class="post-metadata">

### Author: ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)
#### Post date: [June 1, 2020, 7:33am UTC](https://discourse.julialang.org/t/whats-the-proper-way-to-create-and-initialize-a-vector-of-vectors-of-matrices-and-vectors/40515/7 "2020-06-01T07:33:03Z")

</div>

I would also go with @Henrique_Becker’s solution: define a structure `Simulation` with perhaps a struct’s field containing the simulation parameters and another field with a `Vector{Run}`s (this would be your second inner vector).

Struct `Run` contains a `Vector{Float64}` with the simulation solution (and one field with `Matrix{Float64}`, if the case be). If each subset needs additional information, you can add capture this as an extra field to `Run`.

The reason not to go with Vector{Vector{Vector}} is because more sooner than later, you will find yourself in the position of having forgotten how the data is saved and what results belongs to what simulation.

```julia
struct Run
  vector_result::Vector{Float64}
  matrix_result::Matrix{Float64}
end

struct Simulation
  #name::String
  parameters
  runs::Vector{Run}
end

s = Simulation(some_parameters, Vector{Run}())

```

At the end of each run, you construct a `Run` like this `r = Run(vector_results_from_simulation_run, matrix_result)` and then you save the run in the simulation: `push!(simulation.runs, r)`.

---

<div class="post-metadata">

### Author: ![kai](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@kai](https://discourse.julialang.org/u/kai)
#### Post date: [June 1, 2020, 5:58pm UTC](https://discourse.julialang.org/t/whats-the-proper-way-to-create-and-initialize-a-vector-of-vectors-of-matrices-and-vectors/40515/8 "2020-06-01T17:58:04Z")

</div>

@Henrique_Becker @mzaffalon Thank you both for your suggestion. It makes so much sense to have two types rather than having many layers of vectors. I’ll go with this route. Thanks again!! (Too bad I can only make one reply the solution, hope you don’t mind)

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [June 1, 2020, 6:04pm UTC](https://discourse.julialang.org/t/whats-the-proper-way-to-create-and-initialize-a-vector-of-vectors-of-matrices-and-vectors/40515/9 "2020-06-01T18:04:30Z")

</div>

> [@kai](#):
>
> (Too bad I can only make one reply the solution, hope you don’t mind)

😆. No problem. @mzafallon answer was better exemplified than mine, and most of the time people forget to even like the reply 😅.
