# How to initialize array of arrays with specific type?

**URL:** <https://discourse.julialang.org/t/how-to-initialize-array-of-arrays-with-specific-type/94557>\
**Category:** General Usage\
**Tags:** type, arrays, interpolations, interpolationsjl\
**Created:** [February 13, 2023, 1:15pm UTC](https://discourse.julialang.org/t/how-to-initialize-array-of-arrays-with-specific-type/94557 "2023-02-13T13:15:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![CeterisPartybus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ceterispartybus/32/46868_2.png) [@CeterisPartybus](https://discourse.julialang.org/u/CeterisPartybus)\
**Post date:** [February 13, 2023, 1:15pm UTC](https://discourse.julialang.org/t/how-to-initialize-array-of-arrays-with-specific-type/94557/1 "2023-02-13T13:15:58Z")

</div>

Hi everyone,

I would like to initialize an array that is later filled with interpolation objects. To be specific, it should store output from interpolate() from Interpolations.jl later on.

Normally, I can initialize any array of arrays like this:

```julia
A = fill([], 1, 20)

```

or if I want to have the inner arrays to be `Float64`, I can write

```julia
A = fill(Float64[], 1, 20)

```

Julia tells me that an interpolation object is of type `Interpolations.BSplineInterpolation{Float64, 2, OffsetArrays.OffsetMatrix{Float64, Matrix{Float64}}, BSpline{Cubic{Line{OnGrid}}}, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}}`

Therefore, I try to initialize my array like this

```julia
A = fill(Interpolations.BSplineInterpolation{Float64, 2, OffsetArrays.OffsetMatrix{Float64, Matrix{Float64}}, BSpline{Cubic{Line{OnGrid}}}, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}}, 1, 20)

```

That looks fine, but when I try to store an interpolation afterwards in one of the arrays like this.

```julia
B = rand(10,10)
A[1] = interpolate( B, BSpline(Cubic(Line(OnGrid()))))

```

I get this error

```julia
ERROR: MethodError: Cannot `convert` an object of type Interpolations.BSplineInterpolation{Float64, 2, OffsetMatrix{Float64, Matrix{Float64}}, BSpline{Cubic{Line{OnGrid}}}, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}} to an object of type DataType

```

What am I doing wrong?

Thanks a lot!

Here is the complete MWE:

```julia
using Interpolations

# Initialize array of interpolation objects
A = fill(Interpolations.BSplineInterpolation{Float64, 2, OffsetArrays.OffsetMatrix{Float64, Matrix{Float64}}, BSpline{Cubic{Line{OnGrid}}}, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}}, 1, 20)

# Set up some "data" for interpolations
B = rand(10,10)

# Try to store an interpolation in one of the elements of A
A[1] = interpolate( B, BSpline(Cubic(Line(OnGrid()))))

```

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [February 13, 2023, 1:26pm UTC](https://discourse.julialang.org/t/how-to-initialize-array-of-arrays-with-specific-type/94557/2 "2023-02-13T13:26:59Z")

</div>

As far as I can tell you’re missing a pair of brackets. Cf

```julia
julia> A = fill(Float64, 1, 20)
1×20 Matrix{DataType}:
 Float64 Float64 Float64 Float64 Float64 Float64 … Float64 Float64 Float64 Float64 Float64

julia> A[1] = [1.0, 2.0]
ERROR: MethodError: Cannot `convert` an object of type Vector{Float64} to an object of type DataType
Closest candidates are:
  convert(::Type{T}, ::T) where T at ~/julia1.7/share/julia/base/essentials.jl:218
Stacktrace:
 [1] setindex!(A::Matrix{DataType}, x::Vector{Float64}, i1::Int64)
   @ Base ./array.jl:903
 [2] top-level scope
   @ REPL[5]:1

```

---

<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:** [February 13, 2023, 1:27pm UTC](https://discourse.julialang.org/t/how-to-initialize-array-of-arrays-with-specific-type/94557/3 "2023-02-13T13:27:53Z")

</div>

What you are using to fill the array `A` is the `DataType` of the interpolation. To use `A[1] = ...` you need an array of instances of the interpolation. For example:

```julia
julia> b = interpolate( B, BSpline(Cubic(Line(OnGrid()))));

julia> A = fill(copy(b), 10);

julia> A[1] = interpolate( B, BSpline(Cubic(Line(OnGrid()))));

```

Otherwise you may just want an empty array of interpolations, such as:

```julia
julia> A = typeof(b)[]
Interpolations.BSplineInterpolation{Float64, 2, OffsetMatrix{Float64, Matrix{Float64}}, BSpline{Cubic{Line{OnGrid}}}, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}}[]

julia> push!(A, interpolate( B, BSpline(Cubic(Line(OnGrid())))))
1-element Vector{Interpolations.BSplineInterpolation{Float64, 2, OffsetMatrix{Float64, Matrix{Float64}}, BSpline{Cubic{Line{OnGrid}}}, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}}}:
 [0.6382324838466641 0.15737452960821852 … 0.39607517204553827 0.15889649188608557; 0.13177398720760136 0.6391502891054832 … 0.9749632710156909 0.23570550771235294; … ; 0.26655297042787274 0.3298620168301782 … 0.09981476823205598 0.3372852663412892; 0.4963880643359818 0.10982508349889288 … 0.792355741208348 0.5513251663389037]

```

but then you need to `push!` to it.

(of if you really want an array of empty arrays of interpolations, `A = fill(typeof(b)[], 10,20)`, in which case you need `push!(A[1], interpolate(...))`. )

---

<div class="post-metadata">

**Author:** ![CeterisPartybus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ceterispartybus/32/46868_2.png) [@CeterisPartybus](https://discourse.julialang.org/u/CeterisPartybus)\
**Post date:** [February 13, 2023, 1:30pm UTC](https://discourse.julialang.org/t/how-to-initialize-array-of-arrays-with-specific-type/94557/4 "2023-02-13T13:30:12Z")

</div>

Thanks, that could be one mistake. So, do you mean I should add `[]` behind the type when using `fill()`?  
I tried this and it returns the same error:

```julia
A = fill(Interpolations.BSplineInterpolation{Float64, 2, OffsetArrays.OffsetMatrix{Float64, Matrix{Float64}}, BSpline{Cubic{Line{OnGrid}}}, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}}[], 1, 20)

```

---

<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:** [February 13, 2023, 1:32pm UTC](https://discourse.julialang.org/t/how-to-initialize-array-of-arrays-with-specific-type/94557/5 "2023-02-13T13:32:20Z")

</div>

> [@CeterisPartybus](#):
>
> So, do you mean I should add `[]` behind the type when using `fill()`?

Yes, then the next step is to `push!` to the first array of the list:

```julia
push!(A[1], interpolate( B, BSpline(Cubic(Line(OnGrid())))))

```

(the array is empty, so you cannot simply assign to one of its elements:

```julia
julia> v = Float64[]
Float64[]

julia> x[1] = 0
ERROR: MethodError: no method matching _checklubounds(::Bool, ::Tuple{Int64}, ::Tuple{Int64}, ::Tuple{})

```

---

<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:** [February 13, 2023, 1:34pm UTC](https://discourse.julialang.org/t/how-to-initialize-array-of-arrays-with-specific-type/94557/6 "2023-02-13T13:34:00Z")

</div>

> [@CeterisPartybus](#):
>
> Normally, I can initialize any array of arrays like this:
> 
> ```julia
> A = fill([], 1, 20)
> 
> ```

Note that this initializes every element to the _same_ empty array. If you want to initialize them to _distinct_ empty arrays you should use a comprehension. See [Initialize array of arrays - #2 by GunnarFarneback](https://discourse.julialang.org/t/initialize-array-of-arrays/11610/2)

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [February 13, 2023, 1:41pm UTC](https://discourse.julialang.org/t/how-to-initialize-array-of-arrays-with-specific-type/94557/7 "2023-02-13T13:41:33Z")

</div>

> [@CeterisPartybus](#):
>
> I tried this and it returns the same error:

No, it’s a different error. 🙂 It really depends on what you want to do. For example this works now (and avoids the problem that you initialized all elements in the outer array with the same vector):

```julia
A[1] = [interpolate( B, BSpline(Cubic(Line(OnGrid()))))]

```

But it’s quite possible that you want and/or should do something different.

---

<div class="post-metadata">

**Author:** ![CeterisPartybus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ceterispartybus/32/46868_2.png) [@CeterisPartybus](https://discourse.julialang.org/u/CeterisPartybus)\
**Post date:** [February 13, 2023, 1:46pm UTC](https://discourse.julialang.org/t/how-to-initialize-array-of-arrays-with-specific-type/94557/8 "2023-02-13T13:46:26Z")

</div>

Well, that is quite important! I did not know that. Thanks!
