Append to 2d array

In a 1-D allocatable array, you can append to it with

integer, allocatable :: arr(:)

allocate(arr(0))
arr = [arr, 9]

Is the same sort of thing possible in a 2-D array?

integer, allocatable :: arr(:,:)

allocate(arr(2,0))
arr = [arr, [9,10]]

In the code above I would want arr to end up having size [2,1], with entries [9,10]

Thanks!

2 Likes

See this thread where I suggest RESHAPE intrinsic.

You can use RESHAPE with your example, I personally think that is the better option and one I recommend with intrinsic types :

   integer, allocatable :: arr(:,:)
   allocate( arr(2,0) )
   arr = reshape( arr, shape=[2,1], pad=[9,10] )
   print *, arr
end
4 Likes