0

I have a two-dimensional NumPy array with shape

(2, 2)

Example array

array([[1, 2], [3, 4]])

I am trying to have it copy on just the first axis until it reaches the shape:

(5, 2)

Example result array

array([[1, 2], [1, 2], [1, 2], [3, 4], [3, 4]])

np.repeat does the job but it has to be a multiple as it repeats everything

np.repeat(arr, 3, axis=0)

array([[1, 2], [1, 2], [1, 2], [3, 4], [3, 4], [3, 4]])

giving a 6 by 2 array and not a 5 by 2 array

1 Answer 1

1
np.repeat(arr, [3, 2], axis=0)
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, but I am looking to repeat the entire array 3 times not each array a specific amount. I have a very large array with many more axis so I am looking for a method to repeat the entire array like the normal np.repeat(arr, no, axis) but crop the remaining part for example if I want to repeat a (1475,2) array to reach (3500, 2)
sorry for the confusing question, a normal crop can work after repeat now that I think about it but if there is a method that would be helpful
@AzariaGebremichael I guess cropping is the best way, you could dinamically build an array of repeats to give to np.repeat but I guess it's overengineering and it will probably be slower than cropping. Something like: q, r = np.divmod(3500, 3); repeats = [3] * q + [r] + [0] * (len(arr) - q - 1); newarr = np.repeat(arr, repeats, axis=0)

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.