Manualmsdos
Manualmsdos

Reputation: 1547

How to change every even sequences in numpy array

I want to change every even two elements in my array, for example:

arr = np.arange(0, 10)
arr

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

mask = np.concatenate(np.split(np.arange(0, 10),5)[::2])   
arr[mask] += 100
arr

array([100, 101, 2, 3, 104, 105, 6, 7, 108, 109])

Is there a simple way to do it?

Upvotes: 0

Views: 146

Answers (1)

Divakar
Divakar

Reputation: 221584

One way would be with modulus -

arr[(np.arange(len(arr))//2)%2==0] += 100

Upvotes: 3

Related Questions