Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to flip numpy array along the diagonal efficiently? [duplicate]

Tags:

python

numpy

flip

Lets say that i have the following array (note that there is a 1 in the [2,0] position and a 2 in the [3,4] position):

[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[1, 0, 0, 0, 0]
[0, 0, 0, 0, 2]
[0, 0, 0, 0, 0]

and I want to flip it along the diagonal efficiently such that:

[0, 0, 1, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 0, 2, 0]

This does not work with fliplr or rot90 or flipud. Would like efficient answer rather than just an answer since unfortunately this is not being performed on matrices this small.

like image 455
negfrequency Avatar asked Apr 13 '20 23:04

negfrequency


People also ask

How do you flip a numpy array vertically?

You can flip the image vertically and horizontally by using numpy. flip() , numpy. flipud() , numpy. fliplr() .

How do I flip a numpy array horizontally?

Flip an array vertically (axis=0). Flip an array horizontally (axis=1). flip(m, 0) is equivalent to flipud(m). flip(m, 1) is equivalent to fliplr(m).

How do you flip the shape of a numpy array?

The numpy. flip() function reverses the order of array elements along the specified axis, preserving the shape of the array. Parameters : array : [array_like]Array to be input axis : [integer]axis along which array is reversed.

How do I reverse the order of a numpy array?

The flip() method is used to reverse the order of values in an array along the given axis. The flip() method is defined under the numpy library, which can be imported as import numpy as np, and we can create multidimensional arrays and create other mathematical statistics with the help of the Python numpy module.

What is flip in NumPy?

numpy.flip¶ numpy.flip(m, axis=None)[source]¶ Reverse the order of elements in an array along the given axis. The shape of the array is preserved, but the elements are reordered. New in version 1.12.0.

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 do you flip an input array in Python?

Input array. axisNone or int or tuple of ints, optional Axis or axes along which to flip over. The default, axis=None, will flip over all of the axes of the input array. If axis is negative it counts from the last to the first axis.

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.


1 Answers

Both np.rot90(np.fliplr(x)) and transposing the array solves this.

a = np.random.uniform(size=(5,5))
a.T == np.rot90(np.fliplr(a))
like image 80
negfrequency Avatar answered Oct 31 '22 20:10

negfrequency