0

I am fairly new to python and I am trying to get around understanding the following code:

import numpy as np
n=4
matrix=np.zeros((n,n))
for j in range (0,n):
    for i in range (n-1,n-j-2,-1):
        matrix[i,j]=2*n-i-j-1
print (matrix)

I would greatly appreciate if someone could please help me understand how each line executes and how the code is revaluated with the loop.

Thanks in advance!

3
  • In short: It creates a 4 by 4 array where each cell in the lower left triangle has the value i - j + 1 where i is the row index and j is the column index. You need to be more specific about what you don't understand if that does not solve the issue. Commented Nov 21, 2018 at 23:30
  • Thanks for your prompt reply! My question is how do I know that the lower left triangle is the one where the matrix formula executes. Sorry if it is way too obvious, but I am a complete begginner Commented Nov 21, 2018 at 23:33
  • The outer for loop iterates over the row indices. The inner for loop iterates over the columns, but only up to the column number that is equal to the current row number. Commented Nov 21, 2018 at 23:34

2 Answers 2

2

You can add the following print statement, and the loop will explain itself at each iteration:

n=4
matrix=np.zeros((n,n))
for i in range (0,n):
    for j in range(0,i+1):
        print(f'inserting {i-j+1} into the matrix at row index {i}, columns index {j}')
        matrix[i,j]=i-j+1

When you run it, you get this output:

inserting 1 into the matrix at row index 0, columns index 0
inserting 2 into the matrix at row index 1, columns index 0
inserting 1 into the matrix at row index 1, columns index 1
...
inserting 3 into the matrix at row index 3, columns index 1
inserting 2 into the matrix at row index 3, columns index 2
inserting 1 into the matrix at row index 3, columns index 3

And your matrix is populated as before:

>>> matrix
array([[1., 0., 0., 0.],
       [2., 1., 0., 0.],
       [3., 2., 1., 0.],
       [4., 3., 2., 1.]])

Just for reference:

>>> matrix
array([[1., 0., 0., 0.],   #<- "row" index 0
       [2., 1., 0., 0.],   #<- "row" index 1
       [3., 2., 1., 0.],   #<- "row" index 2
       [4., 3., 2., 1.]])  #<- "row" index 3

      # ^      ...  ^
      # "col" 0     "col" 3
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, this was really helpful!!
1
import numpy as np
n=4

We start by setting a 4x4 matrix with all coordinates set to null:

matrix=np.zeros((n,n))         

We set new coordinate values by looping through rows and columns. First we loop through rows, from index 0 to n-1:

for i in range (0,n): 

We next loop through columns. Now, notice that we only loop through those columns whose index is smaller than or equal to that of the current row (i.e., from 0 to i). This way we make sure that the values we set are on or below the diagonal of the matrix:

    for j in range(0,i+1):     

Finally, we set the desired value for the current coordinate:

        matrix[i,j]=i-j+1
print(matrix)

Comments

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.