escorciav
escorciav

Reputation: 85

repeat elements of an array according to other array with the same length? like repelem for numpy

Is there a built-in function or vectorized-efficient combination to repeat each element on an array u based on the element on an array v. It would be something like repelem of Matlab (with array inputs).

Example:

a = np.array([1, 5, 2])
b = np.array([2, 3, 4]) # be must have same length of a
print np.repelem(a, b)

[1, 1, 5, 5, 5, 2, 2, 2, 2]

Upvotes: 3

Views: 3191

Answers (1)

hpaulj
hpaulj

Reputation: 231385

repeat(a, repeats) should do the job

In [21]: np.repeat(np.array([1,5,2]), np.array([2,3,4]))
Out[21]: array([1, 1, 5, 5, 5, 2, 2, 2, 2])

Upvotes: 3

Related Questions