Fourier
Fourier

Reputation: 455

Appending/Merging 2D Arrays

Is it possible to merge 2D Arrays in Python using numpy or something else ? I have about 200 2D arrays , all with the same Dimensions (1024,256) and want to add them to the lower end of each other. The final shape after adding e.g. 3 of them then should be (1024,768).

Upvotes: 1

Views: 6785

Answers (2)

lebenf
lebenf

Reputation: 1028

It's quite simple indeed, provided all arrays are the same size.

>>> a = [[0,1,2],[3,4,5]]
>>> b = [[6,7,8],[9,10,11]]
>>> c = [a[i]+b[i] for i in xrange(len(a))]
>>> c
[[0, 1, 2, 6, 7, 8], [3, 4, 5, 9, 10, 11]]

or better

sum2darray = lambda a, b:  [a[i]+b[i] for i in xrange(len(a))]
c = sum2darray(a,b)

Upvotes: 0

eumiro
eumiro

Reputation: 212835

Three arrays of (1024,256) have to be appended to the right end, not the lower end. You are stacking them horizontally next to each other (1024 rows, 256 columns).

Using numpy.hstack (h as horizontal):

lst is a list of (numpy or python) arrays (1024,256):

numpy.hstack(lst)

returns a single numpy array (1024,256*len(lst))

Upvotes: 7

Related Questions