1

I have 10 different matrix of size (60, 100). I want to put them along the third dimension inside a for loop, so that the final shape is (10, 60, 100).

I tried with concatenate and end up with size (600, 100).

3
  • show us the code you tried Commented Mar 29, 2019 at 10:22
  • Possible duplicate of Expanding NumPy array over extra dimension Commented Mar 29, 2019 at 10:23
  • I have 10 different matrix of size(60,100) Commented Mar 29, 2019 at 10:31

3 Answers 3

3

numpy.stack will allow you to concatenate along new axis.

arrays = [mat1,mat2,.....]
out=np.stack(arrays, axis=0)
Sign up to request clarification or add additional context in comments.

Comments

0

Try creating a new array that you fill with your 2D arrays

new3DArray = numpy.empty(10, 60, 100)

Comments

0

@MahmoudGeMy answer is my favorite. But there is also another way for reference:

out = np.concatenate((mat1[np.newaxis,:],mat1[np.newaxis,:],...),axis=0)

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.