Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting the opposite diagonal of a numpy array

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])
like image 754
Ryan Saxe Avatar asked Apr 19 '13 22:04

Ryan Saxe


People also ask

How do you Inverse an array in NumPy?

We use numpy. linalg. inv() function to calculate the inverse of a matrix. The inverse of a matrix is such that if it is multiplied by the original matrix, it results in identity matrix.

How do you find the secondary diagonal of a matrix?

The primary diagonal is formed by the elements A00, A11, A22, A33. Condition for Principal Diagonal: The row-column condition is row = column. The secondary diagonal is formed by the elements A03, A12, A21, A30. Condition for Secondary Diagonal: The row-column condition is row = numberOfRows – column -1.

Is it possible to write to the diagonal in NumPy?

In NumPy 1.7 and 1.8, it continues to return a copy of the diagonal, but depending on this fact is deprecated. Writing to the resulting array continues to work as it used to, but a FutureWarning is issued. Starting in NumPy 1.9 it returns a read-only view on the original array.

How to write to the original array of diagonals in Python?

The default value is 1. It returns an array of diagonals for a given array ‘a’ as per the offset and axis specified. This function will return read-only view of the original array. To be able to write to the original array you can use numpy.diagonal (a).copy ()

How to find the sum of diagonal elements in NumPy matrix?

Numpy provides us the facility to compute the sum of different diagonals elements using numpy.trace () and numpy.diagonal () method. Method 1: Finding the sum of diagonal elements using numpy.trace () Syntax : numpy.trace (a, offset=0, axis1=0, axis2=1, dtype=None, out=None) Example 1: For 3X3 Numpy matrix

How do you return the diagonal of a 2D array?

Return specified diagonals. If a is 2-D, returns the diagonal of a with the given offset, i.e., the collection of elements of the form a [i, i+offset]. If a has more than two dimensions, then the axes specified by axis1 and axis2 are used to determine the 2-D sub-array whose diagonal is returned.


2 Answers

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
like image 167
unutbu Avatar answered Oct 10 '22 07:10

unutbu


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]
like image 3
Bi Rico Avatar answered Oct 10 '22 06:10

Bi Rico