Python Forum
Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python
#1
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)
Reply
#2
You should try: https://numpy.org/doc/stable/reference/g...numpy-flip
Jeremy7 likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
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)
Jeremy7 likes this post
Reply
#4
(Feb-22-2021, 08:46 AM)DeaD_EyE Wrote: You should try: https://numpy.org/doc/stable/reference/g...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
Reply
#5
(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!
Reply
#6
@Jeremy7 User titles are inserted automatically. They are references to Monty Python's Flying Circus which gave its name to the Python language.
Reply
#7
(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.
Reply
#8
(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.]])
Reply
#9
(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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Elegant way to apply each element of an array to a dataframe? sawtooth500 5 193 8 hours ago
Last Post: deanhystad
  Concatenate array for 3D plotting armanditod 1 191 Mar-21-2024, 08:08 PM
Last Post: deanhystad
  Convert numpy array to image without loading it into RAM. DreamingInsanity 7 5,727 Feb-08-2024, 09:38 AM
Last Post: paul18fr
  How Write Part of a Binary Array? Assembler 1 306 Jan-14-2024, 11:35 PM
Last Post: Gribouillis
  This result object does not return rows. It has been closed automatically dawid294 5 683 Jan-10-2024, 10:55 PM
Last Post: deanhystad
  How is pandas modifying all rows in an assignment - python-newbie question markm74 1 653 Nov-28-2023, 10:36 PM
Last Post: deanhystad
  Loop over an an array of array Chendipeter 1 529 Nov-28-2023, 06:37 PM
Last Post: deanhystad
  How to remove some elements from an array in python? gohanhango 9 985 Nov-28-2023, 08:35 AM
Last Post: Gribouillis
  IPython errors for numpy array min/max methods muelaner 1 508 Nov-04-2023, 09:22 PM
Last Post: snippsat
  How to insert Dashed Lines in between Rows of a tabulate output Mudassir1987 0 463 Sep-27-2023, 10:09 AM
Last Post: Mudassir1987

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020