# How to initialize an array of AbstractArray？

**URL:** <https://discourse.julialang.org/t/how-to-initialize-an-array-of-abstractarray/48022>\
**Category:** General Usage\
**Tags:** question, array\
**Created:** [October 9, 2020, 9:03am UTC](https://discourse.julialang.org/t/how-to-initialize-an-array-of-abstractarray/48022 "2020-10-09T09:03:58Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![F-YF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/f-yf/32/17363_2.png) [@F-YF](https://discourse.julialang.org/u/F-YF)\
**Post date:** [October 9, 2020, 9:03am UTC](https://discourse.julialang.org/t/how-to-initialize-an-array-of-abstractarray/48022/1 "2020-10-09T09:03:58Z")

</div>

How do I initialize a one-dimensional array, where each element of the array is a two-dimensional array?  
For example, I need to initialize a one-dimensional array of length three :[[0 0, 0 0], [0 0 0, 0 0 0], [0 0 0, 0 0 0]], What should I do?

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [October 9, 2020, 9:20am UTC](https://discourse.julialang.org/t/how-to-initialize-an-array-of-abstractarray/48022/2 "2020-10-09T09:20:00Z")

</div>

Here is one way:

```julia
julia> x = Vector{Matrix{Float64}}(undef, 3)
3-element Array{Array{Float64,2},1}:
 #undef
 #undef
 #undef

julia> x[1] = zeros(2,2);

julia> x[2] = zeros(3,3);

julia> x[3] = zeros(3,3);

julia> x
3-element Array{Array{Float64,2},1}:
 [0.0 0.0; 0.0 0.0]
 [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0]
 [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0]

```

---

<div class="post-metadata">

**Author:** ![F-YF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/f-yf/32/17363_2.png) [@F-YF](https://discourse.julialang.org/u/F-YF)\
**Post date:** [October 9, 2020, 9:30am UTC](https://discourse.julialang.org/t/how-to-initialize-an-array-of-abstractarray/48022/3 "2020-10-09T09:30:22Z")

</div>

Ok, thank you very much!

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 9, 2020, 11:44am UTC](https://discourse.julialang.org/t/how-to-initialize-an-array-of-abstractarray/48022/4 "2020-10-09T11:44:54Z")

</div>

> [@F-YF](#):
>
> For example, I need to initialize a one-dimensional array of length three :[[0 0, 0 0], [0 0 0, 0 0 0], [0 0 0, 0 0 0]], What should I do?

PS. If you’re working with lots of small (2x2 or 3x3) arrays _and they are all the same size_, consider an array of [StaticArrays](https://github.com/JuliaArrays/StaticArrays.jl) instead:

```julia
julia> using StaticArrays

julia> zeros(SMatrix{3,3,Float64,9}, 3)
3-element Array{SArray{Tuple{3,3},Float64,2,9},1}:
 [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0]
 [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0]
 [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0]

```

They are much more efficient to work with than regular arrays for such small sizes.

(Note that neither this nor `Vector{Matrix{Float64}}(undef, 3)` is an “array of `AbstractArray`” as your title suggested — they are arrays of specific (“concrete”) `AbstractArray` subtypes. If you really want an array of `AbstractArray`, you could do what @fredrikekre suggested but with `Vector{AbstractArray}(undef, 3)`. This has the advantage that the elements can be _any_ type of array, but the disadvantage that it will be much slower to work with since the types aren’t known until runtime.)

---

<div class="post-metadata">

**Author:** ![F-YF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/f-yf/32/17363_2.png) [@F-YF](https://discourse.julialang.org/u/F-YF)\
**Post date:** [October 9, 2020, 2:08pm UTC](https://discourse.julialang.org/t/how-to-initialize-an-array-of-abstractarray/48022/5 "2020-10-09T14:08:52Z")

</div>

> [@stevengj](#):
>
> they are arrays of specific (“concrete”) `AbstractArray` subtypes.

Thank you for your comments and corrections

---

<div class="post-metadata">

**Author:** ![F-YF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/f-yf/32/17363_2.png) [@F-YF](https://discourse.julialang.org/u/F-YF)\
**Post date:** [October 9, 2020, 2:32pm UTC](https://discourse.julialang.org/t/how-to-initialize-an-array-of-abstractarray/48022/6 "2020-10-09T14:32:00Z")

</div>

> [@fredrikekre](#):
>
> `x = Vector{Matrix{Float64}}(undef, 3)`

The advantage of this approach is that it supports any type of element, and the array dimensions of each element can be different. But it cannot be initialized by the zeros function, or even do the following：

> julia\>x = Vector{Matrix{Float64}}(undef, 3)  
> julia\> x[1] = [1 1;1 1];  
> julia\> x[2] = [2 2 2;2 2 2];  
> julia\> x[3] = [3 3 3;3 3 3; 3 3 3];  
> julia\>x  
> 3-element Array{Array{Float64,2},1}:  
> [1.0 1.0; 1.0 1.0]  
> [2.0 2.0 2.0; 2.0 2.0 2.0]  
> [3.0 3.0 3.0; 3.0 3.0 3.0; 3.0 3.0 3.0]  
> .  
> julia\>zeros(x)  
> MethodError: no method matching zeros(::Array{Array{Float64,2},1})  
> Closest candidates are:  
> zeros(!Matched::Union{Integer, AbstractUnitRange}…) at array.jl:520  
> zeros(!Matched::Type{T}, !Matched::Union{Integer, AbstractUnitRange}…) where T at array.jl:521  
> zeros(!Matched::Tuple{Vararg{Union{Integer, AbstractUnitRange},N} where N}) at array.jl:522  
> …  
> Stacktrace:  
> [1] top-level scope at In[7]:1  
> [2] include\_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1091

> [@stevengj](#):
>
> If you’re working with lots of small (2x2 or 3x3) arrays _and they are all the same size_ , consider an array of [StaticArrays](https://github.com/JuliaArrays/StaticArrays.jl) instead

This approach supports zeros() functions, but does not allow each element to be defined as a different dimension  
So , is there any way to support both?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 9, 2020, 4:48pm UTC](https://discourse.julialang.org/t/how-to-initialize-an-array-of-abstractarray/48022/7 "2020-10-09T16:48:01Z")

</div>

> [@F-YF](#):
>
> [[0 0, 0 0], [0 0 0, 0 0 0], [0 0 0, 0 0 0]]

Lets say you have a list of the sizes of each array, then you can use a comprehension:

```julia
julia> l = [(2,2), (3,3), (3,3)];

julia> m = [zeros(s) for s in l]
3-element Array{Array{Float64,2},1}:
 [0.0 0.0; 0.0 0.0]
 [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0]
 [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0]

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 9, 2020, 7:10pm UTC](https://discourse.julialang.org/t/how-to-initialize-an-array-of-abstractarray/48022/8 "2020-10-09T19:10:59Z")

</div>

> [@F-YF](#):
>
> This approach supports zeros() functions, but does not allow each element to be defined as a different dimension  
> So , is there any way to support both?

If the arrays don’t all have the same size, then you shouldn’t use StaticArrays (you _can_, but you will lose the main benefits and it might even be slower because of the type-instability). The point of StaticArrays is to exploit the case where the array size is _static_ (fixed at compile-time).

---

<div class="post-metadata">

**Author:** ![F-YF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/f-yf/32/17363_2.png) [@F-YF](https://discourse.julialang.org/u/F-YF)\
**Post date:** [October 10, 2020, 2:39am UTC](https://discourse.julialang.org/t/how-to-initialize-an-array-of-abstractarray/48022/9 "2020-10-10T02:39:42Z")

</div>

That’s a good idea! Thank you very much!
