0

I have a 2D numpy array

import numpy as np
np.array([[1, 2], [3, 4]])
[[1 2]
 [3 4]]

I would like the above array to be enlarged to the following:

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

Each element in the original array because a 2x2 array in the new array.

I do I go from the first array to the second 'enlarged' array?

Edit

This is different from the question about scaling here How to "scale" a numpy array? because np.kron(a, np.ones((2,2))) is not the same as a.repeat(2, axis=1).repeat(2, axis=0)

Edit #2 still waiting to get the [duplicate] flag removed

As posted by @Michael Szczesny. The numpy API has seen a slight change. This question is relevant as updated answers are being provide.

2
  • 1
    The solution I just posted is also in the linked question. Commented Dec 21, 2021 at 19:59
  • 1
    It was. I don't know when it changed. You can use np.ones((2,2), 'bool'). Commented Dec 21, 2021 at 19:59

1 Answer 1

2

You can use np.repeat twice, with axis=1 and axis=0:

out = a.repeat(2, axis=1).repeat(2, axis=0)

Output:

>>> out
array([[1, 1, 2, 2],
       [1, 1, 2, 2],
       [3, 3, 4, 4],
       [3, 3, 4, 4]])
Sign up to request clarification or add additional context in comments.

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.