Python Forum
Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python (/thread-32621.html)



Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python - Jeremy7 - Feb-22-2021

How do you get indexing [::-1] to reverse ALL 2D array rows and ALL 3D and 4D array columns and rows simultaneously? I can only get indexing [::-1] to reverse 2D array columns. Python

import numpy as np

randomArray = np.round(10*np.random.rand(5,4))
sortedArray = np.sort(randomArray,axis=1)
reversedArray = sortedArray[::-1]
# reversedArray = np.flip(sortedArray,axis=1)

print('Random Array:')
print(randomArray,'\n')
print('Sorted Array:')
print(sortedArray,'\n')
print('Reversed Array:')
print(reversedArray)



RE: Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python - DeaD_EyE - Feb-22-2021

You should try: https://numpy.org/doc/stable/reference/generated/numpy.flip.html#numpy-flip


RE: Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python - Gribouillis - Feb-22-2021

Not a numpy expert here, but this seems to reverse all the axis simultaneously
import numpy as np
 
random_array = np.round(10 * np.random.rand(5, 4, 4))
sorted_array = np.sort(random_array,axis=1)
 
a = sorted_array.reshape(sorted_array.size)
reversed_array = a[::-1].reshape(sorted_array.shape)

print('Sorted Array:')
print(sorted_array,'\n')
print('Reversed Array:')
print(reversed_array)



RE: Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python - Jeremy7 - Feb-22-2021

(Feb-22-2021, 08:46 AM)DeaD_EyE Wrote: You should try: https://numpy.org/doc/stable/reference/generated/numpy.flip.html#numpy-flip
Thanks for your help; I'm aware of numpy.flip(). numpy.flip() works great, but I wanted to only use the index. I got the following answers with some help from Stack Overflow and another source:
[:,::-1] reverses 2D array rows, [:,::-1,:] reverses 3D array columns, [:,:,::-1] reverses 3D array rows, [:,:,::-1,:] reverses 4D array columns, [:,:,:,::-1] reverses 4D array rows


RE: Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python - Jeremy7 - Feb-22-2021

(Feb-22-2021, 08:54 AM)Gribouillis Wrote: Not a numpy expert here, but this seems to reverse all the axis simultaneously
import numpy as np
 
random_array = np.round(10 * np.random.rand(5, 4, 4))
sorted_array = np.sort(random_array,axis=1)
 
a = sorted_array.reshape(sorted_array.size)
reversed_array = a[::-1].reshape(sorted_array.shape)

print('Sorted Array:')
print(sorted_array,'\n')
print('Reversed Array:')
print(reversed_array)
Thanks for your help. I got the following answers with some help from Stack Overflow and another source:
[:,::-1] reverses 2D array rows, [:,::-1,:] reverses 3D array columns, [:,:,::-1] reverses 3D array rows, [:,:,::-1,:] reverses 4D array columns, [:,:,:,::-1] reverses 4D array rows

Gribouillis, I see that you're a Moderator. When I view threads and posts, under my username, Jeremy7, are the words "Programmer named Tim," but my name is not Tim. Respectfully request "Programmer named Tim" be removed. Thanks!


RE: Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python - Gribouillis - Feb-22-2021

@Jeremy7 User titles are inserted automatically. They are references to Monty Python's Flying Circus which gave its name to the Python language.


RE: Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python - Jeremy7 - Feb-22-2021

(Feb-22-2021, 01:00 PM)Gribouillis Wrote: @Jeremy7 User titles are inserted automatically. They are references to Monty Python's Flying Circus which gave its name to the Python language.
Ok.


RE: Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python - nilamo - Feb-24-2021

(Feb-22-2021, 11:22 AM)Jeremy7 Wrote: Thanks for your help. I got the following answers with some help from Stack Overflow and another source:
[:,::-1] reverses 2D array rows, [:,::-1,:] reverses 3D array columns, [:,:,::-1] reverses 3D array rows, [:,:,::-1,:] reverses 4D array columns, [:,:,:,::-1] reverses 4D array rows

I think you can achieve what you're looking for by using raw slice objects.
>>> import numpy as np
>>> randomArray = np.round(10*np.random.rand(5,4))
>>> sortedArray = np.sort(randomArray, axis=1)
>>> sortedArray.shape
(5, 4)
>>> axes = len(sortedArray.shape)
>>> slices = tuple(slice(None, None, -1) for _ in range(axes))
>>> reversedArr = sortedArray[slices]
>>> sortedArray
array([[4., 6., 6., 7.],
       [3., 6., 7., 8.],
       [1., 8., 9., 9.],
       [7., 8., 8., 9.],
       [0., 6., 7., 8.]])
>>> reversedArr
array([[8., 7., 6., 0.],
       [9., 8., 8., 7.],
       [9., 9., 8., 1.],
       [8., 7., 6., 3.],
       [7., 6., 6., 4.]])



RE: Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python - Jeremy7 - Mar-02-2021

(Feb-24-2021, 10:57 PM)nilamo Wrote:
(Feb-22-2021, 11:22 AM)Jeremy7 Wrote: Thanks for your help. I got the following answers with some help from Stack Overflow and another source:
[:,::-1] reverses 2D array rows, [:,::-1,:] reverses 3D array columns, [:,:,::-1] reverses 3D array rows, [:,:,::-1,:] reverses 4D array columns, [:,:,:,::-1] reverses 4D array rows

I think you can achieve what you're looking for by using raw slice objects.
>>> import numpy as np
>>> randomArray = np.round(10*np.random.rand(5,4))
>>> sortedArray = np.sort(randomArray, axis=1)
>>> sortedArray.shape
(5, 4)
>>> axes = len(sortedArray.shape)
>>> slices = tuple(slice(None, None, -1) for _ in range(axes))
>>> reversedArr = sortedArray[slices]
>>> sortedArray
array([[4., 6., 6., 7.],
       [3., 6., 7., 8.],
       [1., 8., 9., 9.],
       [7., 8., 8., 9.],
       [0., 6., 7., 8.]])
>>> reversedArr
array([[8., 7., 6., 0.],
       [9., 8., 8., 7.],
       [9., 9., 8., 1.],
       [8., 7., 6., 3.],
       [7., 6., 6., 4.]])
Thanks, nilamo!