How to get matrix specific diagonal with numpy in Python

1 Answer

0 votes
import numpy as np

matrix = np.arange(16).reshape((4,4))
print(matrix)

d = np.diag(matrix, k = 1)
print("\n", d)



'''
run:

[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]

 [ 1  6 11]

'''

 



answered Nov 17, 2022 by avibootz
...