I am trying to loop through a matrix to replace every number along the diagonal (top left to bottom right), with -5. I have read previous questions and answers and I see that you can use… np.fill_diagonal(A, -5) to the get the answer. However I am trying to use a loop with if statements. Can anyone help me get started? Here is my matrix.
A = array([[1.2,3.4,10.3],[2,8,78],[45,-36,8]])
If I got it right, you want to iterate over columns and rows, so (quite silly, indeed):
Although I have to think this is not needed if you ALREADY have an array/matrix, then using Mgilson’s answer or, better yet,
numpy.fill_diagonal(array, value).