Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5966945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:52:19+00:00 2026-05-22T19:52:19+00:00

Similar to this answer , I have a pair of 3D numpy arrays, a

  • 0

Similar to this answer, I have a pair of 3D numpy arrays, a and b, and I want to sort the entries of b by the values of a. Unlike this answer, I want to sort only along one axis of the arrays.

My naive reading of the numpy.argsort() documentation:

Returns
-------
index_array : ndarray, int
    Array of indices that sort `a` along the specified axis.
    In other words, ``a[index_array]`` yields a sorted `a`.

led me to believe that I could do my sort with the following code:

import numpy

a = numpy.zeros((3, 3, 3))
a += numpy.array((1, 3, 2)).reshape((3, 1, 1))
print "a"
print a
"""
[[[ 1.  1.  1.]
  [ 1.  1.  1.]
  [ 1.  1.  1.]]

 [[ 3.  3.  3.]
  [ 3.  3.  3.]
  [ 3.  3.  3.]]

 [[ 2.  2.  2.]
  [ 2.  2.  2.]
  [ 2.  2.  2.]]]
"""
b = numpy.arange(3*3*3).reshape((3, 3, 3))
print "b"
print b
"""
[[[ 0  1  2]
  [ 3  4  5]
  [ 6  7  8]]

 [[ 9 10 11]
  [12 13 14]
  [15 16 17]]

 [[18 19 20]
  [21 22 23]
  [24 25 26]]]
"""
print "a, sorted"
print numpy.sort(a, axis=0)
"""
[[[ 1.  1.  1.]
  [ 1.  1.  1.]
  [ 1.  1.  1.]]

 [[ 2.  2.  2.]
  [ 2.  2.  2.]
  [ 2.  2.  2.]]

 [[ 3.  3.  3.]
  [ 3.  3.  3.]
  [ 3.  3.  3.]]]
"""

##This isnt' working how I'd like
sort_indices = numpy.argsort(a, axis=0)
c = b[sort_indices]
"""
Desired output:

[[[ 0  1  2]
  [ 3  4  5]
  [ 6  7  8]]

 [[18 19 20]
  [21 22 23]
  [24 25 26]]

 [[ 9 10 11]
  [12 13 14]
  [15 16 17]]]
"""
print "Desired shape of b[sort_indices]: (3, 3, 3)."
print "Actual shape of b[sort_indices]:"
print c.shape
"""
(3, 3, 3, 3, 3)
"""

What’s the right way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T19:52:20+00:00Added an answer on May 22, 2026 at 7:52 pm

    You still have to supply indices for the other two dimensions for this to work correctly.

    >>> a = numpy.zeros((3, 3, 3))
    >>> a += numpy.array((1, 3, 2)).reshape((3, 1, 1))
    >>> b = numpy.arange(3*3*3).reshape((3, 3, 3))
    >>> sort_indices = numpy.argsort(a, axis=0)
    >>> static_indices = numpy.indices((3, 3, 3))
    >>> b[sort_indices, static_indices[1], static_indices[2]]
    array([[[ 0,  1,  2],
            [ 3,  4,  5],
            [ 6,  7,  8]],
    
           [[18, 19, 20],
            [21, 22, 23],
            [24, 25, 26]],
    
           [[ 9, 10, 11],
            [12, 13, 14],
            [15, 16, 17]]])
    

    numpy.indices calculates the indices of each axis of the array when “flattened” through the other two axes (or n – 1 axes where n = total number of axes). In other words, this (apologies for the long post):

    >>> static_indices
    array([[[[0, 0, 0],
             [0, 0, 0],
             [0, 0, 0]],
    
            [[1, 1, 1],
             [1, 1, 1],
             [1, 1, 1]],
    
            [[2, 2, 2],
             [2, 2, 2],
             [2, 2, 2]]],
    
    
           [[[0, 0, 0],
             [1, 1, 1],
             [2, 2, 2]],
    
            [[0, 0, 0],
             [1, 1, 1],
             [2, 2, 2]],
    
            [[0, 0, 0],
             [1, 1, 1],
             [2, 2, 2]]],
    
    
           [[[0, 1, 2],
             [0, 1, 2],
             [0, 1, 2]],
    
            [[0, 1, 2],
             [0, 1, 2],
             [0, 1, 2]],
    
            [[0, 1, 2],
             [0, 1, 2],
             [0, 1, 2]]]])
    

    These are the identity indices for each axis; when used to index b, they recreate b.

    >>> b[static_indices[0], static_indices[1], static_indices[2]]
    array([[[ 0,  1,  2],
            [ 3,  4,  5],
            [ 6,  7,  8]],
    
           [[ 9, 10, 11],
            [12, 13, 14],
            [15, 16, 17]],
    
           [[18, 19, 20],
            [21, 22, 23],
            [24, 25, 26]]])
    

    As an alternative to numpy.indices, you could use numpy.ogrid, as unutbu suggests. Since the object generated by ogrid is smaller, I’ll create all three axes, just for consistency sake, but note unutbu’s comment for a way to do this by generating only two.

    >>> static_indices = numpy.ogrid[0:a.shape[0], 0:a.shape[1], 0:a.shape[2]]
    >>> a[sort_indices, static_indices[1], static_indices[2]]
    array([[[ 1.,  1.,  1.],
            [ 1.,  1.,  1.],
            [ 1.,  1.,  1.]],
    
           [[ 2.,  2.,  2.],
            [ 2.,  2.,  2.],
            [ 2.,  2.,  2.]],
    
           [[ 3.,  3.,  3.],
            [ 3.,  3.,  3.],
            [ 3.,  3.,  3.]]])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for an answer to a question similar to this one: protect users'
This question is similar to mine, but does not contain an answer. I want
I have read thru a number of other questions similar to this one -
My current problem is very similar to this one . I have a downloadFile(URL)
This question is very similar to this one . However, the accepted answer (and
I have a question similar to this one: CGLayerRef in NSValue - when to
I have trawled the internet for this answer, but alas couldn't find one. In
This topic can look similar to others but I haven't found any usable answer
My question is similar to this one but none of the answers solve my
A client has asked me to make their YouTube channel look similar this one:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.