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
#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


Messages In This Thread
RE: Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python - by Jeremy7 - Feb-22-2021, 11:22 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Writing a cycle to find the nearest point from the array Tysrusko 0 191 May-10-2024, 11:49 AM
Last Post: Tysrusko
  This result object does not return rows. It has been closed automatically dawid294 6 1,356 Mar-30-2024, 03:08 AM
Last Post: NolaCuriel
  Elegant way to apply each element of an array to a dataframe? sawtooth500 7 529 Mar-29-2024, 05:51 PM
Last Post: deanhystad
  Concatenate array for 3D plotting armanditod 1 318 Mar-21-2024, 08:08 PM
Last Post: deanhystad
  Convert numpy array to image without loading it into RAM. DreamingInsanity 7 6,025 Feb-08-2024, 09:38 AM
Last Post: paul18fr
  How Write Part of a Binary Array? Assembler 1 426 Jan-14-2024, 11:35 PM
Last Post: Gribouillis
  How is pandas modifying all rows in an assignment - python-newbie question markm74 1 763 Nov-28-2023, 10:36 PM
Last Post: deanhystad
  Loop over an an array of array Chendipeter 1 628 Nov-28-2023, 06:37 PM
Last Post: deanhystad
  How to remove some elements from an array in python? gohanhango 9 1,419 Nov-28-2023, 08:35 AM
Last Post: Gribouillis
  IPython errors for numpy array min/max methods muelaner 1 624 Nov-04-2023, 09:22 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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