# Initialize nested arrays

**URL:** <https://discourse.julialang.org/t/initialize-nested-arrays/89284>\
**Category:** General Usage\
**Created:** [October 26, 2022, 8:28am UTC](https://discourse.julialang.org/t/initialize-nested-arrays/89284 "2022-10-26T08:28:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ManuelFossa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manuelfossa/32/36305_2.png) [@ManuelFossa](https://discourse.julialang.org/u/ManuelFossa)\
**Post date:** [October 26, 2022, 8:28am UTC](https://discourse.julialang.org/t/initialize-nested-arrays/89284/1 "2022-10-26T08:28:23Z")

</div>

I need to initialize a Matrix, containing Matrices, the latter containing a Vector or Matrices.

I did something like this:

```julia
sizeMat11 = 2
sizeMat12 = 3
sizeMat21 = 5
sizeMat22 = 6
sizeVec31 = 9
struct SomeStruct 
    x::Float64 
end
function SomeFunction(l::Int64 , n::Int64 ,m::Int64)::Tuple{Array{Float64,2},Array{Float64,2},Vector{Float64}}
    a = zeros(l,l)
    b = zeros(m,m)
    c = rand(n)
return a,b,c
end

v = Array{Array{Vector{AbstractMatrix{SomeStruct}}, 2} , 2}(undef , sizeMat11 , sizeMat12)
for imat11 = 1:sizeMat11
   for imat12 = 1:sizeMat12
      for imat21 = 1:sizeMat21
          for imat22 = 1:sizeMat22
              a,b,c = SomeFunction(10,15,9)
              v[imat11,imat12][imat21,imat22] = [a,b,c]
           end
      end
   end
end

```

I get an undef error.  
I consulted [isbits type undef error](https://discourse.julialang.org/t/initializing-array-of-arrays-with-undef-gives-undefreferror/53266) post, but so far no success.

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [October 26, 2022, 8:46am UTC](https://discourse.julialang.org/t/initialize-nested-arrays/89284/2 "2022-10-26T08:46:00Z")

</div>

protip, if you don’t care about flow control like `continue` and `break`, you can write nested loops really easily with:

```julia
for i=1:I, j=1:J, k=1:K
    # blah
end

```

the `j` loop is the most tightly nested, and the `i` loop is the outer loop.

anyway, I’m too lazy to check if this has the dimensions you’re looking for, but you might consider this:

```julia
v = [[SomeFunction(10,15,9) for imat21=1:sizeMat21, imat22=1:sizeMat22] for imat12=1:sizeMat12, imat21=1:sizeMat21]

```

you can also make a 4-d array, in case that makes things easier in any way:

```julia
v = [SomeFunction(10,15,9) for imat21=1:sizeMat21, imat22=1:sizeMat22, imat12=1:sizeMat12, imat21=1:sizeMat21]

```

list comprehensions have the nice feature of not needing to think through entirely ahead of time the proper allocation structure.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 26, 2022, 9:13am UTC](https://discourse.julialang.org/t/initialize-nested-arrays/89284/3 "2022-10-26T09:13:26Z")

</div>

Yeah, this won’t work. Here’s a simpler example:

```julia
julia> a = Vector{Vector{Int}}(undef, 2)
2-element Vector{Vector{Int64}}:
 #undef
 #undef

julia> a[1][1] = 3
ERROR: UndefRefError: access to undefined reference

```

You haven’t allocated any inner vector that you can write to, the outer vector just contains two `undef` references. You have to pre-allocate the innermost arrays, before you can start accessing and writing to them. Or you can write `a[1] = [1,2,3]`, but you cannot index into an `undef`, only replace it with a value.

The easiest way is normally to use comprehensions, like @uniment said, then you will also get correct element types. You should definitely avoid having an `AbstractMatrix` eltype in there, that will harm performance.

---

<div class="post-metadata">

**Author:** ![Tbl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbl/32/5984_2.png) [@Tbl](https://discourse.julialang.org/u/Tbl)\
**Post date:** [October 26, 2022, 9:21am UTC](https://discourse.julialang.org/t/initialize-nested-arrays/89284/4 "2022-10-26T09:21:24Z")

</div>

I would like also to point out that if the sizes of the matrices/vectors are constant, then there is no need to make them nested. You can just make higher dimensional one.

---

<div class="post-metadata">

**Author:** ![ManuelFossa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manuelfossa/32/36305_2.png) [@ManuelFossa](https://discourse.julialang.org/u/ManuelFossa)\
**Post date:** [October 27, 2022, 5:32am UTC](https://discourse.julialang.org/t/initialize-nested-arrays/89284/5 "2022-10-27T05:32:44Z")

</div>

Thank you, it works 🙂  
Thanks to everyone
