# How to initialize array with one unknow dimension size

**URL:** <https://discourse.julialang.org/t/how-to-initialize-array-with-one-unknow-dimension-size/72045>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 25, 2021, 7:51am UTC](https://discourse.julialang.org/t/how-to-initialize-array-with-one-unknow-dimension-size/72045 "2021-11-25T07:51:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oto\_Brzobohaty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oto_brzobohaty/32/10370_2.png) [@Oto\_Brzobohaty](https://discourse.julialang.org/u/Oto_Brzobohaty)\
**Post date:** [November 25, 2021, 7:51am UTC](https://discourse.julialang.org/t/how-to-initialize-array-with-one-unknow-dimension-size/72045/1 "2021-11-25T07:51:47Z")

</div>

I am not able to find any example how to initialize 2D array with one unknown dimension.

Normally I use:

```julia
A = zeros(dim1,dim2)

for ii = 1:dim2
    A[:,ii] = some_function_returning_vector_of_dim1()

end

```

But very often I don’t know the dim1.

Please, could you help me?

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [November 25, 2021, 8:49am UTC](https://discourse.julialang.org/t/how-to-initialize-array-with-one-unknow-dimension-size/72045/2 "2021-11-25T08:49:06Z")

</div>

I don’t think you can do that with base `Array`: arrays with dimension \>1 cannot be resized, so you need to specify the size when it is created. What you can do of course is start with an array of size 0xn and use `hcat` to “grow” the array.

Maybe there’s a fancy array type that can do this concatenation without allocation… Actually even a `DataFrame` might do the job, depending on your needs…

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [November 25, 2021, 9:17am UTC](https://discourse.julialang.org/t/how-to-initialize-array-with-one-unknow-dimension-size/72045/3 "2021-11-25T09:17:53Z")

</div>

You can start with a vector, append elements to that and then `reshape` it to the dimensions you need.

There’s a package which does essentially this: [GitHub - JuliaArrays/ElasticArrays.jl: Resizeable multi-dimensional arrays for Julia](https://github.com/JuliaArrays/ElasticArrays.jl)
