0

I have three arrays

a = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) #Original large array
b = np.array([4, 8, 5]) #Smaller array
c = np.array([2, 7, 9]) #Arguments

The result should look like the following

np.array([4, 4, 4, 8, 8, 8, 8, 8, 5, 5])

This means that [0, 1, 2] are replaced by 4, [3, 4, 5, 6, 7] are replaced by 8 and [8, 9] are placed by 5. Is there any numpy function/code for that?

1
  • b.repeat(np.diff(c,prepend=-1))? Commented Jul 23, 2020 at 19:30

1 Answer 1

1

I am not aware if such a function exists but the function down below gets the job done:

for i, element in enumerate(a):
if a[i] <= c[x]:
    a[i] = b[x]
else:
    x+=1
    a[i] = b[x]
Sign up to request clarification or add additional context in comments.

1 Comment

I was specifically looking for a numpy answer. Since my array is really large then the vectorization gives much better run time compared to for loop.

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.