How to "spread" a numpy array (opposite of slice with step size)?

Question:

Is there a way to spread the values of a numpy array? Like an opposite to slicing with a step size > 1:

>>> a = np.array([[1, 0, 2], [0, 0, 0], [3, 0, 4]])
>>> a
array([[1, 0, 2],
       [0, 0, 0],
       [3, 0, 4]])

>>> b = a[::2, ::2]
>>> b
array([[1, 2],
       [3, 4]])

In this example, is there an elegant way to get a from b?

Asked By: trivicious

||

Answers:

You can create a zeros array with correct shape first and then assign with step size:

import numpy as np
b = np.array([[1, 2], [3, 4]])
a = np.zeros((b.shape[0] * 2 - 1, b.shape[1] * 2 - 1), dtype='int')
a[::2, ::2] = b
a
# array([[1, 0, 2],
#        [0, 0, 0],
#        [3, 0, 4]])
Answered By: Psidom