Natasha
Natasha

Reputation: 111

How do I copy a slice of a matrix and paste it to the end of the same matrix in Python?

I have a matrix of size (21, 15, 50) and I want to take the last level of the matrix which would be a size (1, 15, 50) and I want to copy that matrix slice and paste it back to the original so then the original matrix would have a size of (22, 15, 50). This would result in slices 21 and 22 to have the same values.

I have tried np.hstack() and append() but I could be using them incorrectly.

What is the syntax for this? Thank you!

Upvotes: 0

Views: 148

Answers (1)

Phuong-Uyen Phan-Thi
Phuong-Uyen Phan-Thi

Reputation: 66

I call your 3d array is a.

This code may solve your problem:

a = np.concatenate((a, a[-1,:,:].reshape(1,15,50)), axis=0)

I have tried and checked the shape of a, the result is (22,15,50) as you want.

Upvotes: 1

Related Questions