rougier / numpy-100

100 numpy exercises (with solutions)
MIT License
12.17k stars 5.74k forks source link

about question 18 #192

Closed hacshyac closed 1 year ago

hacshyac commented 1 year ago

Is "diagonal" not a line in two ways in a rectangle? One way is from left top to right down. Another way is from right top to left down. Or I understand the diagonal in matrix wrongly.. Then just ignore this issue.

If my guess is right, i would give a suggestion for an answer (using clumsy indexing way, not so clever):

z = np.zeros((5, 5), int)
z[1, 0], z[1, -1] = 1, 1
z[2, 1], z[2, -2] = 2, 2
z[3, 2], z[3, -3] = 3, 3
z[4, 3], z[4, -4] = 4, 4
print(z)

output is:

[[0 0 0 0 0]
 [1 0 0 0 1]
 [0 2 0 2 0]
 [0 0 3 0 0]
 [0 4 0 4 0]]
rougier commented 1 year ago

For matrix, it is usually the diagonal of the identity matrix.