0

here is my situation. Suppose you have an array A of some elements, A is np.ndarray, and B - also np.ndarray, where B[i] is how many times I'd like to take element A[i] for the output. Example: A = np.array([1, 2, 3, 4, 5]), B = np.array([0, 0, 2, 1, 3]). The result should be np.array([3, 3, 4, 5, 5, 5]).

So how to make it in numpy fast, without for-loops etc.

Thanks.

Tried to find something appropriate in numpy doc, but no success.

1
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. Commented Dec 10, 2022 at 10:39

1 Answer 1

2

Try np.repeat: np.repeat(A, B)

Sign up to request clarification or add additional context in comments.

2 Comments

what if B is matrix, and I want to make it along rows of B? np repeat won't work this time
@ДенисСтафилов Can you give a minimal example? I'm not sure about an output you expect. It might be array with multiple sizes of rows which is not supported in numpy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.