# Initializing an empty Julia array with unknown size

**URL:** <https://discourse.julialang.org/t/initializing-an-empty-julia-array-with-unknown-size/76967>\
**Category:** General Usage\
**Tags:** question\
**Created:** [February 23, 2022, 11:57am UTC](https://discourse.julialang.org/t/initializing-an-empty-julia-array-with-unknown-size/76967 "2022-02-23T11:57:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [February 23, 2022, 11:57am UTC](https://discourse.julialang.org/t/initializing-an-empty-julia-array-with-unknown-size/76967/1 "2022-02-23T11:57:09Z")

</div>

I have 6 different scenarios, and under each scenario, there could be one to 100 models available for me to get the data. My goal is to store them in `A` as below (n is an unknown number between 1 and 100):

```julia
for i in 1:6 # Go through all of the scenarios
   # go through the 100 models
   A = []; # the step I need help.
   n = 0;
   for j = 1:100
      if a model is available 
          n .+ 1
          A[n] = Model n (35 grids, each with a size of 360 x 180);
       end
   end
   # other calculations that requires A
end

```

Here is my question. Under each of the scenario, I would need to reset my A to empty values. How do I do that? In Matlab, I can just do the below:  
`A = [];`  
Unfortunately, it does not work for Julia, as we know, Julia needs to know the exact size of the A, which is unknown in this example. How should I deal with this situation?

Many thanks!

---

<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 23, 2022, 12:04pm UTC](https://discourse.julialang.org/t/initializing-an-empty-julia-array-with-unknown-size/76967/2 "2022-02-23T12:04:53Z")

</div>

Use `push!` to add elements to the array:

```julia
julia> x = Matrix{Float64}[] # an empty array of matrices (better than [])
Matrix{Float64}[]

julia> push!(x,[1.0 2.0 ; 3.0 4.0])
1-element Vector{Matrix{Float64}}:
 [1.0 2.0; 3.0 4.0]

```

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [February 23, 2022, 12:08pm UTC](https://discourse.julialang.org/t/initializing-an-empty-julia-array-with-unknown-size/76967/3 "2022-02-23T12:08:08Z")

</div>

This addresses my issue well 👍 👍 Thank you very much!

> [@lmiq](#):
>
> `x = Matrix{Float64}[]`

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [February 23, 2022, 12:18pm UTC](https://discourse.julialang.org/t/initializing-an-empty-julia-array-with-unknown-size/76967/4 "2022-02-23T12:18:25Z")

</div>

May I ask one more question?

Actually, my model output is not a matrix with the size of 360x180. It is 35 matrices with the size of 360x180, like this: `Model1[1] = grid1, Model1[2] = grid2, ..., Model1[35] = grid35.`

In that case, how do I modify the below to fit my purpose?  
`x = Matrix{Float64}[]`

Using the above directly gives me the below error:

```julia
LoadError: MethodError: no method matching Matrix{Float64}(::Vector{Any})
Closest candidates are:
  Array{T, N}(::AbstractArray{S, N}) where {T, N, S}

```

Many thanks!

---

<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 23, 2022, 12:23pm UTC](https://discourse.julialang.org/t/initializing-an-empty-julia-array-with-unknown-size/76967/5 "2022-02-23T12:23:43Z")

</div>

One would need to see exactly what is the type of `grid`.

But you can use `x = []` and just add anything there, it is just that this is of type Any and may have some performance effect later (although all this is highly speculative with the information we have here).

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [February 23, 2022, 12:26pm UTC](https://discourse.julialang.org/t/initializing-an-empty-julia-array-with-unknown-size/76967/6 "2022-02-23T12:26:38Z")

</div>

Thank you! Good to know that.

I just run my code with `typeof` for my 35 layer grid, and I got the below:  
`typeof(Model1) = Vector{Any}`

---

<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 23, 2022, 1:05pm UTC](https://discourse.julialang.org/t/initializing-an-empty-julia-array-with-unknown-size/76967/7 "2022-02-23T13:05:20Z")

</div>

You can use `Vector{Vector{Any}}` then for the container, but more for clarity than for anything, since you are already getting `Any` from the data you are fetching.

---

<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:** [February 23, 2022, 1:32pm UTC](https://discourse.julialang.org/t/initializing-an-empty-julia-array-with-unknown-size/76967/8 "2022-02-23T13:32:51Z")

</div>

Technically, accessing the outer `Vector{Vector{Any}}` is not type-unstable, only accessing a position in one of the inner `Vector` is type-unstable. However, the type may have been deduced as `Vector{Any}` because some of the inner vectors are `Vector{X}` and other `Vector{Y}` (in this case the inner vectors themselves are of specific types and the outer vector must be `Vector{Any}`).

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [February 23, 2022, 1:43pm UTC](https://discourse.julialang.org/t/initializing-an-empty-julia-array-with-unknown-size/76967/9 "2022-02-23T13:43:00Z")

</div>

> [@leon](#):
>
> `x = Matrix{Float64}[]`
> 
> Using the above directly gives me the below error:
> 
> ```julia
> LoadError: MethodError: no method matching Matrix{Float64}(::Vector{Any})
> 
> ```

Are you sure you typed that in correctly? That code is fine, and it shouldn’t produce an error:

```julia
julia> x = Matrix{Float64}[]
Matrix{Float64}[]

```

From the error you posted, it seems like you may have actually done:

```julia
julia> x = Matrix{Float64}([])
ERROR: MethodError: no method matching Matrix{Float64}(::Vector{Any})

```

Note the extra `()`.

For some more background, in Julia you can write: `T[]` to get a vector of elements of type `T`. In other words, these two things are the same:

```julia
julia> Int[]
Int64[]

julia> Vector{Int}()
Int64[]

```

So if you want a collection of matrices, you would substitute `T = Matrix{Float64}` and write:

```julia
julia> Matrix{Float64}[]
Matrix{Float64}[]

```

or, if you prefer:

```julia
julia> Vector{Matrix{Float64}}()
Matrix{Float64}[]

```
