1

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!

1

1 Answer 1

1

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".

Sign up to request clarification or add additional context in comments.

1 Comment

Ahhh yess that was fairly easy, thanks!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.