2

I have an array counts = [2, 3, 4] with each element denoted by c. I want to convert it into an array weights that has m copies of each element c, where m is an element in M = [3, 2, 1].

So in the end, I'd like an array weights = [2, 2, 2, 3, 3, 4]

Is there an efficient way to do this in numpy?

0

1 Answer 1

4

numpy.repeat does exactly what you want:

>>> np.repeat(counts, M)
array([2, 2, 2, 3, 3, 4])
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.