rows should be a column vector e.g.
rows=[[0],[1],[5]] cols=[2,3] A[rows,cols]+=1
Two-stage indexing sometimes works, A[rows][:,cols] , but not always. In particular, this is not the case when rows not a slice. A[rows] now a copy, so changing it does not change A
There are various ways to index this block. plonser's use of the product works, although I rarely saw that it was used with numpy .
np.ix_ is a convenient tool for this:
In [70]: np.ix_([0,1,5],[2,3]) Out[70]: (array([[0], [1], [5]]), array([[2, 3]]))
np.newaxis also turns a row vector into a column:
rows=np.array([0,1,5]) cols=np.array([2,3]) A[rows[:,None],cols]
This pairing of column and row vectors works because numpy passes them to create an array of indices (3,2) .
itertools.product produces the same set of indices, but as a list (or generator) of tuples
In [80]: list(itertools.product([0,1,5],[2,3])) Out[80]: [(0, 2), (0, 3), (1, 2), (1, 3), (5, 2), (5, 3)] In [84]: tuple(np.array(list(itertools.product([0,1,5],[2,3]))).T) Out[84]: (array([0, 0, 1, 1, 5, 5]), array([2, 3, 2, 3, 2, 3]))