WX_M
WX_M

Reputation: 488

Python concatenate with different size arrays

I have a folder with several different variable names, as well as several different descriptors that are different. For example:

 A1, B1, C1, A2, B2, C2, A3, B3, C3, ...

These files have a numpy shape of [#, 5, 5 ,1] where # will be different for each variable and number. However, I want to create a master array of All A's, B's, and C's, which have been concatenated.

An example would be:

 A1.shape = [1426,5,5,1]
 A2.shape = [1322,5,5,1]
 A3.shape = [1112,5,5,1]
 1426 + 1322 + 1112 = 3860
 allA.shape = [3860,5,5,1]

I have tried a couple different ways, including pre-allocating 'allA' as an empty numpy array and concatenating, but the error is such that 'all the input array dimensions except for the concatenation axis must match exactly'. What is the correct, or even, easier way to do this?

Upvotes: 0

Views: 249

Answers (1)

Antonina
Antonina

Reputation: 604

numpy.concatenate should work properly in this case.

import numpy as np

a = np.zeros((10, 5, 5, 1))
b = np.zeros((15, 5, 5, 1))
c = np.zeros((20, 5, 5, 1))

res = np.concatenate((a, b, c), axis=0)  # concatenation

print(res.shape)  # (45, 5, 5, 1)

Upvotes: 1

Related Questions