Python: How to expand a numpy array with adjacent repeating columns?
I'm trying to achieve what I have a matrix like this:
axy
axy
axy
axy
And I want to expand this matrix so that it becomes:
aaaxxxyyy
aaaxxxyyy
aaaxxxyyy
aaaxxxyyy
Is there a function I can easily use to manipulate this transformation? I would like to use a better way than splitting each column separately and adding them back.
Thanks in advance.
+3
source to share
1 answer
You can use np.repeat :
>>> import numpy as np
>>> a = np.arange(9).reshape(3,3)
>>> a
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> b = np.repeat(a, 3, axis=1) # array, times, axis
>>> b
array([[0, 0, 0, 1, 1, 1, 2, 2, 2],
[3, 3, 3, 4, 4, 4, 5, 5, 5],
[6, 6, 6, 7, 7, 7, 8, 8, 8]])
+4
source to share