5

maybe this is a easy question, but is there a fast way to duplicate elements in a array? It should work like this way for 3D:

1 2 3
4 5 6
7 8 9

1 1 2 2 3 3
1 1 2 2 3 3
4 4 5 5 6 6
4 4 5 5 6 6
7 7 8 8 9 9
7 7 8 8 9 9

I tried it with 3 nested for-loops, but this was really slow.

1 Answer 1

6
>>> a = np.array([[1, 2, 3],
                  [4, 5, 6],
                  [7, 8, 9]])
>>> np.repeat(np.repeat(a, 2, 0), 2, 1)

array([[1, 1, 2, 2, 3, 3],
       [1, 1, 2, 2, 3, 3],
       [4, 4, 5, 5, 6, 6],
       [4, 4, 5, 5, 6, 6],
       [7, 7, 8, 8, 9, 9],
       [7, 7, 8, 8, 9, 9]])
Sign up to request clarification or add additional context in comments.

3 Comments

Great Solution! But I figured out that this function change the dtype from float64 to float32. Can I avoid this?
@Christian - it doesn't change the dtype for me.
sorry, my fault - I used another function which destroyed the dtype. thanks! :)

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.