-1

Given you have rows and cols coordinates/indexes, how can I update multiple cells simultaneously... something like :

 rows_idxs = [.....]
 cols_idxs = [.....]
 ary[rows_idxs, cols_idxs] += 1

OR

 ary[itertools.product(rows_idxs,cols_idxs)] += 1

neither of those works !?

How can I do that ?

sten
  • 7,028
  • 9
  • 41
  • 63
  • I’m not sure I understand what you mean. Can you clarify, maybe show an example? – AMC Nov 29 '19 at 20:36

1 Answers1

1

If you know your row and col indices have no duplicate values then the numpy replacement for itertools.product would be np.ix_. Be sure to note the trailing underline.

For example:

a = np.arange(15).reshape(3,5)
a[np.ix_([0,2],[1,3,4])] += 1
a
# array([[ 0,  2,  2,  4,  5],
#        [ 5,  6,  7,  8,  9],
#        [10, 12, 12, 14, 15]])

If there are duplicates you can use it together with np.add.at:

For example:

a = np.arange(15).reshape(3,5)
np.add.at(a,np.ix_([0,0,0,2],[0,0,3,4,4]),1)
a
# array([[ 6,  1,  2,  6, 10],
#        [ 5,  6,  7,  8,  9],
#        [12, 11, 12, 14, 16]])
Paul Panzer
  • 51,835
  • 3
  • 54
  • 99