2

I'm trying to "stack" array a2 below a1 such that I get array b with the following shape

a1.shape => (2, 50, 241)
a2.shape => (50, 241)

# goal
b.shape => (3, 50, 241)

This was my attempt, but np.stack requires the same shape

b = np.stack([a1, a2])

3 Answers 3

2
import numpy as np
arr1 = np.random.rand(2, 50, 241)
arr2 = np.random.rand(50, 241)

Reshape arr2 so it's got the same 3d structure:

arr2 = arr2.reshape(1, 50, 241)

Vstack it:

arr3 = np.vstack((arr1, arr2))

>>> arr3.shape
(3, 50, 241)
Sign up to request clarification or add additional context in comments.

Comments

1

If your arrays are numpy arrays, try np.append

b = np.append(a1, [a2])

This is assuming you are trying to construct b such that a2 is the last item of b and a1 is the first 2 items.

Comments

0

Try using:

a2_reshaped = a2.reshape((1,) + a2.shape)
b = np.stack([a1, a2_reshaped])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.