14

let's say I have this: (numpy array)

a=
[0  1  2  3],
[4  5  6  7],
[8  9 10  11]

to get [1,1] which is 5 its diagonal is zero; according to numpy, a.diagonal(0)= [0,5,10]. How do I get the reverse or the right to left diagonal [2,5,8] for [1,1]? Is this possible? My original problem is an 8 by 8 (0:7).. I hope that helps

2
  • 1
    there are two answers that appear not generalized enough becuase your sample a is small. Do you want the answer for [2,2] to be [7, 10]? related question, can you be rectangular in the other direction (tall not wide)? Commented Nov 23, 2013 at 16:46
  • My example is an 8 by 8 (0:7).. I hope that helps Commented Nov 23, 2013 at 17:26

4 Answers 4

18

Get a new array each row reversed.

>>> import numpy as np
>>> a = np.array([
...     [0, 1, 2, 3],
...     [4, 5, 6, 7],
...     [8, 9, 10, 11]
... ])
>>> a[:, ::-1]
array([[ 3,  2,  1,  0],
       [ 7,  6,  5,  4],
       [11, 10,  9,  8]])
>>> a[:, ::-1].diagonal(1)
array([2, 5, 8])

or using numpy.fliplr:

>>> np.fliplr(a).diagonal(1)
array([2, 5, 8])
Sign up to request clarification or add additional context in comments.

6 Comments

Note for the general case this requires calculating the offset from the other side of the array, e.g. you will need to use a.shape in your calculations
@wim, You're right. Your answer is more elegant in regard to this matter. Thank you for comment. +1
In my program, I dont get to know the "1" inside the diagonal function? How do I get that?
@AhmedNassar, You can use max(a.shape[1]-a.shape[0], 0) instead of 1.
Sorry, I'm confused how can I use this. i might've not been clear, let's say I want to get [3,6,9] what operation should I take to get it straight away, without looking at the table.
|
5

Flip the array upside-down and use the same:

np.flipud(a).diagonal(0)[::-1]

Comments

4

Another way to achieve this is to use np.rot90

import numpy as np

a = np.array([[0,  1,  2,  3],
              [4,  5,  6,  7],
              [8,  9, 10,  11]])            

my_diag = np.rot90(a).diagonal(-1)

Result:

>>> my_diag
array([2, 5, 8])

Comments

0

A number of answers so far. @Akavall is closest as you need to rotate or filip and transpose (equivilant operations). I haven't seen a response from the OP regarding expected behavior on the "long" part of the rectangle.

Generalized solution for a square matrix:

a = array([[ 0,  1,  2,  3,  4],
           [ 5,  6,  7,  8,  9],
           [10, 11, 12, 13, 14],
           [15, 16, 17, 18, 19],
           [20, 21, 22, 23, 24]])
>>> [(i, np.rot90(a).diagonal(2*i-a.shape[0]+1)) for i in range(a.shape[0])]
[(0, array([0])),
 (1, array([ 2,  6, 10])),
 (2, array([ 4,  8, 12, 16, 20])),
 (3, array([14, 18, 22])),
 (4, array([24]))]

As a function:

def reverse_diag(arr, n):
    idx = 2*n - arr.shape[0]+1
    return np.rot90(arr).diagonal(idx)

original matrix can be made square with a[:np.min(a.shape),:np.min(a.shape)]

EDIT: OP indicated the array is square.... Final Answer is the above

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.