I have two (large) arrays. For illustration purposes I'm using a simple example below:
In [14]: arr1 = np.arange(32*512).reshape(32, 512)
In [15]: arr2 = np.arange(512).reshape(1, 512)
And I wanted to do a horizontal concatenation of these arrays (i.e. concatenation along axis 1). I came up with the following approach to achieve this:
In [16]: np.hstack([arr1, np.tile(arr2, (arr1.shape[0], 1))]).shape
Out[16]: (32, 1024)
This works as intended. However, I would like to know whether there are any other efficient ways of doing this concatenation without using numpy.tile
. I'm afraid I would blow-up my memory requirements since the arrays are really huge.
If it's possible avoid this duplication of rows (to match the dimensions of arr1
), maybe using broadcasting, then it'd be great!
P.S. The reason why I want to avoid this copying is because of linear growth of memory requirements:
In [20]: arr2.nbytes
Out[20]: 4096
In [19]: np.tile(arr2, (arr1.shape[0], 1)).nbytes
Out[19]: 131072
In [22]: arr1.shape[0] * arr2.nbytes
Out[22]: 131072