In Matlab, there is the command repelem which works as follows (see https://www.mathworks.com/help/matlab/ref/repelem.html#buocbhj-2):
e.g.: Create a matrix and repeat each element into a 3-by-2 block of a new matrix.
A = [1 2; 3 4]
B = repelem(A,3,2)
A = (2×2)
1 2
3 4
B = (6×4)
1 1 2 2
1 1 2 2
1 1 2 2
3 3 4 4
3 3 4 4
3 3 4 4
What is the best way to do the same in Numpy?
A = np.arange(1,5).reshape((2,2))
B = ...
You can chain np.repeat
specifying axis
repelem = lambda a, x, y: np.repeat(np.repeat(a, x, axis=0), y, axis=1)
# same as repelem = lambda a, x, y: a.repeat(x, 0).repeat(y, 1)
Just call
>>> a = np.array([[1,2], [3,4]])
>>> repelem(a, 3, 2)
array([[1, 1, 2, 2],
[1, 1, 2, 2],
[1, 1, 2, 2],
[3, 3, 4, 4],
[3, 3, 4, 4],
[3, 3, 4, 4]])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With