1

I have a 2d numpy array (2 x 2) elements, I want to create another 2D numpy array out of it, such that:

2D array:

import numpy as np
np.random.rand(2,2)
  array([[10.0,8.0],
              [6.0,4.0]])

I want to get a 4x4 array out of it such that all values of the finer resolution array corresponding to a specific cell of the coarser array, are the same as the coarser array:

  array([[10.0,10.0,8.0,8.0],
            [10.0,10.0,8.0,8.0]
            [6.0,6.0,4.0,4.0]
            [6.0,6.0,4.0,4.0]])

I could do this using for loops, but would really like to find out if a more pythonic way exists.

1 Answer 1

3

You can use repeat:

>>> a = np.random.rand(2,2)
>>> a
array([[ 0.66172561,  0.09262421],
       [ 0.40578266,  0.84510431]])
>>> a.repeat(2, 0).repeat(2, 1)
array([[ 0.66172561,  0.66172561,  0.09262421,  0.09262421],
       [ 0.66172561,  0.66172561,  0.09262421,  0.09262421],
       [ 0.40578266,  0.40578266,  0.84510431,  0.84510431],
       [ 0.40578266,  0.40578266,  0.84510431,  0.84510431]])
Sign up to request clarification or add additional context in comments.

3 Comments

Can your soln be extended to converting a 2x2 nump array to a 8x8 array?
Yes, first argument is the number of repetitions, the second argument is the axis. Array size does not matter.
great, this is super useful

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.