Stack two numpy array of two different shape

Viewed 18

I have two NumPy arrays.

One has the dimension (4,1).

enter image description here

Another numpy array I have with teh dimension of (240, 320) enter image description here

Now, I want to make a CSV file that will look like this: enter image description here

As the two numpy array size is different, how can i stack them ? any suggestion?

1 Answers

I think that you cannot stack two arrays without the same shape according to numpy.stack. However, you can save the arrays sequentially to a csv file to obtain the result you seek. The following code must do the tricks.

import numpy as np

mat0 = np.random.randint(0, 10, (4, 1), dtype=int)
mat1 = np.random.randint(0, 10, (240, 320), dtype=int)

with open('foo.csv', 'w') as f:
    np.savetxt(f, mat0, delimiter=',')
    np.savetxt(f, mat1, delimiter=',')
Related