I have numpy M*N array.
numpy.random.seed(23)
A = numpy.round(numpy.random.random((4, 10)), 2)
array([[0.59, 0.77, 0.66, 0.56, 0.18, 0.24, 0.51, 0.4 , 0.48, 0.96],
[0.9 , 0.51, 0.82, 0.83, 0.23, 0.08, 0.47, 0.88, 0.15, 0.23],
[0.92, 0.13, 0.92, 0.23, 0.62, 0.95, 0.26, 0.45, 0.97, 0.24],
[0.2 , 0.69, 0.85, 0.45, 0.1 , 0.62, 0.08, 0.05, 0.35, 0.91]])
Then I have a list of M lists of indexes:
ind = [[1,2],
[4,7,8,9],
[3,6,7],
[4,5,1]]
Each list contains indexes of the corresponding row to be nulled, i.e. at row #0: 1th and 2nd elements should be nulled, etc...
And I should obtain:
array([[0.59, 0. , 0. , 0.56, 0.18, 0.24, 0.51, 0.4 , 0.48, 0.96],
[0.9 , 0.51, 0.82, 0.83, 0. , 0.08, 0.47, 0. , 0. , 0. ],
[0.92, 0.13, 0.92, 0. , 0.62, 0.95, 0. , 0. , 0.97, 0.24],
[0.2 , 0. , 0.85, 0.45, 0. , 0. , 0.08, 0.05, 0.35, 0.91]])
The forehead solution is:
for i in range(len(ind)):
A[i, ind[i]] = 0
But, you know, it's too slow. Is it possible to have "vectorized" solution?