Context: I have a 2D array A that I would like to modify at specific indices, given by the arrays a1 and a2. I could use a for loop but I want to optimize this problem.
Problem: The way I modify my array needs to use a2 as a slice: A[a1, a2:] = 1000. But I can't manage to get past TypeError: only integer scalar arrays can be converted to a scalar index. How could I do that value replacement of A faster than with loops ?
Example:
import numpy as np
# Initialize array
A = np.zeros((10,10),int)
# Create two arrays of indices
a1 = np.array([1,5,6], dtype = int)
a2 = np.array([4,6,2], dtype = int)
# As a for loop
for i in range(a1.shape[0]):
A[a1[i], a2[i]:] = 10
# What I tried (doesn't work)
A[a1, a2:]
A
Out[452]:
array([[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 10, 10, 10, 10, 10, 10],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 10, 10, 10, 10],
[ 0, 0, 10, 10, 10, 10, 10, 10, 10, 10],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
Your example, tweaked for easier display:
Ranges can be converted to advanced indexing arrays. In this case:
And we can find the '10s' in
Awith:This approach is inspired by various posts about padding lists of various lengths.
But we can't use that to set values in
A. I'll have to do some more experimenting to get that to work.edit
Define another index that covers all rows:
Then make the mask:
Test the fetch:
and the set: