Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an anti-diagonal identity matrix (where the diagonal is flipped left to right) in numpy

How can I create anti-diagonal matrix in numpy? I can surely do it manually, but curious if there is a function for it.

I am looking for a Matrix with the ones going from the bottom left to the upper right and zeros everywhere else.

like image 905
user1700890 Avatar asked May 23 '18 14:05

user1700890


1 Answers

Use np.eye(n)[::-1] which will produce:

array([[ 0.,  0.,  0.,  0.,  1.],
       [ 0.,  0.,  0.,  1.,  0.],
       [ 0.,  0.,  1.,  0.,  0.],
       [ 0.,  1.,  0.,  0.,  0.],
       [ 1.,  0.,  0.,  0.,  0.]])

for n=5

like image 71
jpp Avatar answered Sep 22 '22 05:09

jpp