[Numpy-discussion] "upsample" or scale an array
Derek Homeier
derek at astro.physik.uni-goettingen.de
Sat Dec 3 12:50:18 EST 2011
On 03.12.2011, at 6:22PM, Robin Kraft wrote:
> That does repeat the elements, but doesn't get them into the desired order.
>
> In [4]: print a
> [[1 2]
> [3 4]]
>
> In [7]: np.tile(a, 4)
> Out[7]:
> array([[1, 2, 1, 2, 1, 2, 1, 2],
> [3, 4, 3, 4, 3, 4, 3, 4]])
>
> In [8]: np.tile(a, 4).reshape(4,4)
> Out[8]:
> array([[1, 2, 1, 2],
> [1, 2, 1, 2],
> [3, 4, 3, 4],
> [3, 4, 3, 4]])
>
> It's close, but I want to repeat the elements along the two axes, effectively stretching it by the lower right corner:
>
> array([[1, 1, 2, 2],
> [1, 1, 2, 2],
> [3, 3, 4, 4],
> [3, 3, 4, 4]])
>
> It would take some more reshaping/axis rolling to get there, but it seems doable.
>
> Anyone know what combination of manipulations would work with the result of np.tile?
>
Rolling was the keyword:
np.rollaxis(np.tile(a, 4).reshape(2,2,-1), 2, 1).reshape(4,4))
[[1 1 2 2]
[1 1 2 2]
[3 3 4 4]
[3 3 4 4]]
I leave the generalisation and timing up to you, but it seems for
a = np.arange(M**2).reshape(M,-1)
np.rollaxis(np.tile(a, N**2).reshape(M,N,-1), 2, 1).reshape(M*N,-1)
should do the trick.
Cheers,
Derek
More information about the NumPy-Discussion
mailing list