Here is one way: NumPy broadcasting -
a[(np.arange(a.shape[0])[:,None] + np.arange(a.shape[1]))%2==0] += 1
Explanation: We basically create two arrays that are equivalent to the i-th and j-th iterators. Name them I and J
I = np.arange(a.shape[0]) J = np.arange(a.shape[1])
Now, in order to perform the operation between all possible I and J , we create an extension I to 2D by clicking its elements on the first axis and thereby creating a singleton size along its second axis.
Figuratively, the broadcast effect could be done like this:
I[:,None] : M , 1 J : 1 , N I[:,None] + J : M, N
So the final setup will be -
a[(I[:,None] + J)%2==0] += 1
In other words, avoid comparing with 0 and directly use mod-2 , which will be essentially 0 or 1 -
a += (np.arange(a.shape[0])[:,None]-1 + np.arange(a.shape[1]))%2
You can also use np.ix_ to handle odd and then even install strings, for example:
a[np.ix_(np.arange(0,a.shape[0],2),np.arange(0,a.shape[1],2))] += 1 a[np.ix_(np.arange(1,a.shape[0],2),np.arange(1,a.shape[1],2))] += 1