FJ_Abbasi
FJ_Abbasi

Reputation: 443

merging multiple numpy arrays

i have 3 numpy arrays which store image data of shape (4,100,100).

arr1= np.load(r'C:\Users\x\Desktop\py\output\a1.npy')
arr2= np.load(r'C:\Users\x\Desktop\py\output\a2.npy')
arr3= np.load(r'C:\Users\x\Desktop\py\output\a3.npy')

I want to merge all 3 arrays into 1 array. I have tried in this way:

merg_arr = np.zeros((len(arr1)+len(arr2)+len(arr3), 4,100,100), dtype=input_img.dtype)

now this make an array of the required length but I don't know how to copy all the data in this array. may be using a loop?

Upvotes: 2

Views: 12920

Answers (1)

MB-F
MB-F

Reputation: 23637

This will do the trick:

merge_arr = np.concatenate([arr1, arr2, arr3], axis=0)

np.stack arranges arrays along a new dimension. Their dimensions (except for the first) need to match.

Demo:

arr1 = np.empty((60, 4, 10, 10))
arr2 = np.empty((14, 4, 10, 10))
arr3 = np.empty((6, 4, 10, 10))
merge_arr = np.concatenate([arr1, arr2, arr3], axis=0)
print(merge_arr.shape)  # (80, 4, 10, 10)

Upvotes: 7

Related Questions