3

I have two numpy array matrix A and B of length 32 and shape (32, 32, 3) each . I want to combine them into a new array so that the dimensions of my new array is (2, 32, 32,3).

Using np. concatenate is throwing an error.

Manasi
  • 179
  • 1
  • 8

3 Answers3

4

Use np.stack

def dim0_stack(*arrays):
    return np.stack(arrays, axis = 0)
Daniel F
  • 13,620
  • 2
  • 29
  • 55
1

Another way to do it:

a = np.random.randn(32, 32, 3)
b = np.random.randn(32, 32, 3)
c = np.concatenate([np.expand_dims(a,0), np.expand_dims(b, 0)], axis=0)
print(c.shape)

Because you mentioned using concatenate, I thought to show you how you can use it.

Moher
  • 741
  • 7
  • 17
1

Another more literal method

result = np.zeros((2, A.shape[0], A.shape[1], A.shape[2]))
result[0, :, :, :] = A
result[1, :, :, :] = B
Gulzar
  • 23,452
  • 27
  • 113
  • 201