Gurminder Bharani
Gurminder Bharani

Reputation: 531

Concatenating 2d numpy arrays to a 3d numpy array

I have large set of 2d arrays which are being created with loop.

>>> for x in list_imd:
...     arr = arcpy.RasterToNumPyArray(x)
...     print arr.shape
(129, 135)
(129, 135)
(129, 135)
(129, 135)
(129, 135)
(129, 135)
(129, 135)
(129, 135)
(129, 135)
(129, 135)
(129, 135)
(129, 135)
(129, 135)
(129, 135)
(129, 135)
(129, 135)
(129, 135)
(129, 135)
(129, 135)

I want to convert these 2d arrays into one 3d array.

>>> arr_stacked.shape
(19, 129, 135)

Upvotes: 1

Views: 282

Answers (1)

Ami Tavory
Ami Tavory

Reputation: 76297

Try using the simple numpy.array constructor:

import numpy as np

np.array([arcpy.RasterToNumPyArray(x) for x in list_imd])

Here is an example that works by me:

a = np.array([[1, 2, 3], [3, 4, 5]])

>>> np.array([a, a]).shape
(2, 2, 3)

Upvotes: 3

Related Questions