Home > Enterprise >  Is there a function for changing all row values in a numpy array?
Is there a function for changing all row values in a numpy array?

Time:09-28

I have got a 5 by 5 numpy array and a list of 3 values

a = np.array[([[0,4,5,8,2],
 [2,3,4,1,3],
 [2,3,4,1,3],
 [2,3,4,1,3],
 [2,3,4,1,3]])

BC = np.array([0,3,4])

with this condition, every row and column of the BC must be 0. In this case, the first, fourth and fifth row and column. The output therefore needs to be

[0,0,0,0,0
0,3,4,0,0
0,3,4,0,0
0,0,0,0,0
0,0,0,0,0]

Of course it is possible to do a[0,0] = 0 and then for every place in the matrix, but I want to use a for loop because the original matrix is 12*12. Thanks for your help!

CodePudding user response:

you just have to slice the array then assign it to zeros as follows:

a = np.array[([[0,4,5,8,2],
 [2,3,4,1,3],
 [2,3,4,1,3],
 [2,3,4,1,3],
 [2,3,4,1,3]])

BC = np.array([0,3,4])

a[BC,:] = 0
a[:,BC] = 0

[BC,:] losely translates to "indicies of rows as in BC and all columns".

  • Related