42

So in numpy arrays there is the built in function for getting the diagonal indices, but I can't seem to figure out how to get the diagonal starting from the top right rather than top left.

This is the normal code to get starting from the top left:

>>> import numpy as np
>>> array = np.arange(25).reshape(5,5)
>>> diagonal = np.diag_indices(5)
>>> array
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]])
>>> array[diagonal]
array([ 0,  6, 12, 18, 24])

so what do I use if I want it to return:

array([ 4,  8, 12, 16, 20])

3 Answers 3

52

There is

In [47]: np.diag(np.fliplr(array))
Out[47]: array([ 4,  8, 12, 16, 20])

or

In [48]: np.diag(np.rot90(array))
Out[48]: array([ 4,  8, 12, 16, 20])

Of the two, np.diag(np.fliplr(array)) is faster:

In [50]: %timeit np.diag(np.fliplr(array))
100000 loops, best of 3: 4.29 us per loop

In [51]: %timeit np.diag(np.rot90(array))
100000 loops, best of 3: 6.09 us per loop
5
  • 4
    You started the timing thing, so here's my best shot at making it fast: step = len(array) - 1; np.take(array, np.arange(step, array.size, step))
    – Jaime
    Apr 19, 2013 at 23:32
  • @Jaime: That's great -- much faster than my solution. Perhaps we need np.arange(step, array.size-1, step) however? Please post it as a solution so I can vote it up.
    – unutbu
    Apr 19, 2013 at 23:48
  • 2
    I have Tim Peters' The Zen of Python hanging on my cube wall, just off my monitor. I cannot post the code of the comment as an answer while readability counts is looking at me... :P Your solution with fliplr is probably the best: fast enough and much, much more understandable when you revisit it a couple of months after writing it.
    – Jaime
    Apr 19, 2013 at 23:56
  • @Jaime you will always loose with those timings, because diagonal creates a view (or will in newer versions).
    – seberg
    Apr 20, 2013 at 9:23
  • @Jaime - I have no qualms about the zen-iness of my solution: np.diag(arr[:, ::-1] ;) - see my answer below!
    – n1k31t4
    Jul 6, 2018 at 10:16
4

Here is a simple way using numpy slicing. I personally find it not too hard on the eyes, but agree that fliplr is more descriptive.

Just to highlight this example's contribution to existing answers, I ran the same simple benchmark:

In [1]: import numpy as np

In [3]: X = np.random.randint(0, 10, (5, 5))

In [4]: X
Out[4]: 
array([[7, 2, 7, 3, 7],
       [8, 4, 5, 9, 6],
       [0, 2, 9, 0, 4],
       [8, 2, 1, 0, 3],
       [3, 1, 0, 7, 0]])

In [5]: Y = X[:, ::-1]

In [6]: Z1 = np.diag(Y)

In [7]: Z1
Out[7]: array([7, 9, 9, 2, 3])

Parity comparison to fastest alternative:

In [8]: step = len(X) - 1

In [9]: Z2 = np.take(X, np.arange(step, X.size-1, step))

In [10]: Z2
Out[10]: array([7, 9, 9, 2, 3])

In [11]: np.array_equal(Z1, Z2)
Out[11]: True

Benchmarks

In [12]: %timeit np.diag(X[:, ::-1])
1.92 µs ± 29.5 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [13]: %timeit step = len(X) - 1; np.take(X, np.arange(step, X.size-1, step))
2.21 µs ± 246 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

Initial comparisons indicate that my solution is additionally linear in complexity, while using the second 'step' solution is not:

In [14]: big_X = np.random.randint(0, 10, (10000, 10000))

In [15]: %timeit np.diag(big_X[:, ::-1])
2.15 µs ± 96.3 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [16]: %timeit step = len(big_X) - 1; np.take(big_X, np.arange(step, big_X.size-1, step))
100 µs ± 1.85 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

I generally use this method to either flip images (mirroring them), or to convert between opencv's format of (channels, height, width) to matplotlib's format of (height, width , channels). So for a three dimensional image, it'd simply be flipped = image[:, :, ::-1]. Of course you can generalise it to flip along any dimension, by putting the ::-1 part in the desired dimension.

3

Here are two ideas:

step = len(array) - 1

# This will make a copy
array.flat[step:-step:step]

# This will make a veiw
array.ravel()[step:-step:step]
1
  • The second might make a copy ;)
    – seberg
    Apr 20, 2013 at 9:25

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.