-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 ?

1
  • I’m not sure I understand what you mean. Can you clarify, maybe show an example? Commented Nov 29, 2019 at 20:36

1 Answer 1

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]])
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.