Problem
I am trying to repeat the last column in an array (numpy). I wonder if there is a more “elegant” way than resizing an array, copying values and repeating the last line x times.
What i want to achieve
Input Array: Output Array:
[[1,2,3], [[1,2,3,3,3],
[0,0,0], -> repeat(2-times) -> [0,0,0,0,0],
[0,2,1]] [0,2,1,1,1]]
How I solved the problem
x = np.array([[1,2,3],[0,0,0],[0,2,1]])
new_size = x.shape[1] + 2
new_x = np.zeros((3,new_size))
new_x[:,:3] = x
for i in range(x.shape[1],new_size):
new_x[:,i] = x[:,-1]
Another way
Is there a way to solve this problem using the numpy repeat function? Or something shorter or more effective?