rougier/numpy-100

about question 18

hacshyac opened this issue · 1 comments

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]]

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