Skip to content
Advertisement

Get diagonal without using numpy?

I’m trying to get the diagonal from a matrix in Python without using numpy (I really can’t use it). Does someone here knows how to do it?

Example of what I want to get:

JavaScript

Or like:

JavaScript

Until know I’ve tried a lot of stuff but doesn’t work.

JavaScript

If direc==1 I need to get the diagonal that goes from left-> right, top-> bottom.
If direc==-1 need to get the diag that goes from right-> left, top->bottom.

Advertisement

Answer

To get the leading diagonal you could do

JavaScript

or even

JavaScript

And play similar games for other diagonals. For example, for the counter-diagonal (top-right to bottom-left) you would do something like:

JavaScript

For other minor diagonals you would have to use if conditionals in the list comprehension, e.g.:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement