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 6350771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:52:28+00:00 2026-05-24T21:52:28+00:00

I have a 2D masked array of values that I need to sort from

  • 0

I have a 2D masked array of values that I need to sort from lowest to highest. For example:

import numpy as np

# Make a random masked array
>>> ar = np.ma.array(np.round(np.random.normal(50, 10, 20), 1),
                     mask=np.random.binomial(1, .2, 20)).reshape((4,5))
>>> print(ar)
[[-- 51.9 38.3 46.8 43.3]
 [52.3 65.0 51.2 46.5 --]
 [56.7 51.1 -- 38.6 33.5]
 [45.2 56.8 74.1 58.4 56.4]]

# Sort the array from lowest to highest, with a flattened index
>>> sorted_ind = ar.argsort(axis=None)
>>> print(sorted_ind)
[14  2 13  4 15  8  3 11  7  1  5 19 10 16 18  6 17  0 12  9]

But with the sorted indices, I need to divide them into two simple subsets: less than or equal to and greater than or equal to a given datum. Furthermore, I don’t need the masked values, and they need to be removed. For example, with datum = 51.1, how do I filter down sorted_ind to the 10 indices above datum and 8 values below? (Note: there is one shared index due to the or equal to logic criteria. The 3 masked values can be removed from analysis). I need to preserve the flattened index position, as I use np.unravel_index(ind, ar.shape) later on.

  • 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-24T21:52:29+00:00Added an answer on May 24, 2026 at 9:52 pm

    to use where:

    import numpy as np
    np.random.seed(0)
    # Make a random masked array
    ar = np.ma.array(np.round(np.random.normal(50, 10, 20), 1),
                         mask=np.random.binomial(1, .2, 20)).reshape((4,5))
    # Sort the array from lowest to highest, with a flattened index
    sorted_ind = ar.argsort(axis=None)
    
    tmp = ar.flatten()[sorted_ind]
    print sorted_ind[np.ma.where(tmp<=51.0)]
    print sorted_ind[np.ma.where(tmp>=51.0)]
    

    but since tmp is sorted, you can use np.searchsorted():

    tmp = ar.flatten()[sorted_ind].compressed() # compressed() will delete all invalid data.
    idx = np.searchsorted(tmp, 51.0)
    print sorted_ind[:idx]
    print sorted_ind[idx:len(tmp)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The numpy documentation shows an example of masking existing values with ma.masked a posteriori
I have a ArrayList that contains values in the form [ann,john]. I want to
I have code that looks like this: function foobar(array& $objects, $con = null) {
I have a heightmap (a 2D array of floating point values) and I wish
I have a 2D numpy array (i.e matrix) A which contains useful data interspread
I have 3 1-D ndarrays: x, y, z and the following code: import numpy
I have a [20][20] two dimensional array that I've manipulated. In a few words
I have a simple scenario where a panel needs a masked loading indicator over
i have a textbox associated with a calendar extender and a masked edit extender
I've created some MbUnit Test Fixtures that have SetUp methods marked with the SetUp

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.