How to initialize array with one unknow dimension size

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

Normally I use:

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?

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…

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

1 Like