vinu
vinu

Reputation: 453

How to copy a smaller array to a larger array based on a value of another array?

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?

Upvotes: 0

Views: 238

Answers (1)

Kanza Sheikh
Kanza Sheikh

Reputation: 65

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]

Upvotes: 1

Related Questions