# Initializing an empty Julia array with unknown size

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):

``````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!

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

``````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]
``````
1 Like

This addresses my issue well  Thank you very much!

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 = grid1, Model1 = grid2, ..., Model1 = 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:

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

Many thanks!

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).

3 Likes

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}`

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.

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}`).

1 Like

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

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

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

``````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> Int[]
Int64[]

julia> Vector{Int}()
Int64[]
``````

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

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

or, if you prefer:

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