NumPy: Swap rows and columns of a given array in reverse order
Reverse Rows & Columns in Array
Write a NumPy program to swap rows and columns of a given array in reverse order.
This problem involves writing a NumPy program to swap the rows and columns of a given array in reverse order. The task requires utilizing NumPy's array manipulation capabilities, such as slicing and indexing, to efficiently perform row and column swapping operations in reverse order. By rearranging the rows and columns of the original array in reverse, the program generates a modified array with the desired row and column positions exchanged. This facilitates data reorganization for various computational and analytical tasks.
Sample Solution:
Python Code:
Output:
Original array: [[[ 1 2 3 4] [ 0 1 3 4] [90 91 93 94] [ 5 0 3 2]]] Swap rows and columns of the said array in reverse order: [[[ 5 0 3 2] [90 91 93 94] [ 0 1 3 4] [ 1 2 3 4]]] None
Explanation:
In the above code –
np.array([[[1, 2, 3, 4], [0, 1, 3, 4], [90, 91, 93, 94], [5, 0, 3, 2]]]) creates a 3D NumPy array of shape (1, 4, 4) with the specified values.
nums[::-1, ::-1]: The slice operation [::-1] is applied to both the first and second axes (rows and columns), effectively reversing their order.
For more Practice: Solve these Related Problems:
- Reverse both rows and columns of a given array using advanced slicing to simulate a 180-degree rotation.
- Swap rows and columns of a matrix sequentially to produce a reversed order, and compare with np.flipud and np.fliplr results.
- Create a new array by first reversing its columns and then its rows, ensuring the final output matches a complete reversal.
- Implement a function that reverses the order of both rows and columns in an array without using built-in flip functions.
Go to:
PREV : Swap Columns in 4x4 Array
NEXT : Element-Wise Multiply Two Arrays
Python-Numpy Code Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.