Suppose a = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
and s = [3, 3, 9, 3, 6, 3]
. I'm looking for the best way to repeat a[i]
exactly s[i]
times and then have a flatten array in the form of b = [0.1, 0.1, 0.1, 0.2, 0.2, 0.2, 0.3, 0.3, 0.3, 0.3, ... ]
.
I want to do this as fast as possible since I have to do it many times. I'm using Python and numpy and the arrays are defined as numpy.ndarray. I searched around and find out about repeat
, tile
and column_stack
which can be used nicely to repeat each element n
times but I wanted to repeat each of them different times.
One way to do this is:
a = hsplit(a, 6)
for i in range(len(a)):
a[i] = repeat(a[i], s[i])
a = a.flatten()
I am wondering if there is a better way to do it.
np.repeat?
np.repeat
!!! please be careful about your questions validation and clear !!!