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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:10:04+00:00 2026-05-26T05:10:04+00:00

I have a 2D numpy array I want to find the ‘every’ location of

  • 0

I have a 2D numpy array I want to find the ‘every’ location of all the unique elements. We can find the unique elements using numpy.unique(numpyarray.). Here it comes the tricky part. Now I have to know all the locations for every unique element. Lets consider the following example.

array([[1, 1, 2, 2],\
       [1, 1, 2, 2],\
       [3, 3, 4, 4],\
       [3, 3, 4, 4]])

The result should be

1, (0,0),(1,1)
2, (0,2),(1,2)
3, (2,0),(3,1)
4, (2,2),(3,3)

How to do it and what could be a suitable way to store and iterate over the values.

It is to be noted that all the unique values will be adjacent to each other. The only gaps between them could only be zeros. Lets consider another variant

 array([[1, 0, 1, 2, 2],\
        [1, 0, 1, 2, 2],\
        [3, 0, 3, 4, 4],\
        [3, 0, 3, 4, 4]])

The result should be

1, (0,0),(1,2)
2, (0,3),(1,4)
3, (2,0),(3,2)
4, (2,3),(3,4)

The zeoros on the boundaries are to be neglected.

thanks a lot

  • 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-26T05:10:05+00:00Added an answer on May 26, 2026 at 5:10 am

    The simple, brute force way to do it is to just use numpy.where.

    For example, if you’re just wanting the bounding box:

    import numpy as np
    
    x = np.array([[1,1,2,2],
                  [1,1,2,2],
                  [3,3,4,4],
                  [3,3,4,4]])
    
    for val in np.unique(x):
        rows, cols = np.where(x == val)
        rowstart, rowstop = np.min(rows), np.max(rows)
        colstart, colstop = np.min(cols), np.max(cols)
        print val, (rowstart, colstart), (rowstop, colstop) 
    

    This will work for the example with zeros, as well.

    If the array is large, and you already have scipy around, you might consider using scipy.ndimage.find_objects instead, as @unutbu suggested.

    In the particular case of your example, where your unique values are sequential integers, you can use find_objects directly. It expects an array where each sequential integer other than 0 represents an object that it needs to return the bounding box of. (0 is ignored, exactly as you want.) However, in general, you’ll need to do a touch of pre-processing to convert arbitrary unique values to sequential integers.

    find_objects retuns a list of tuples of slice objects. Honestly, these are probably exactly what you want, if you’re wanting the bouding box. However, it will look a bit more messy to print out starting and stopping indicies.

    import numpy as np
    import scipy.ndimage as ndimage
    
    x = np.array([[1, 0, 1, 2, 2],
                  [1, 0, 1, 2, 2],
                  [3, 0, 3, 4, 4],
                  [3, 0, 3, 4, 4]])
    
    for i, item in enumerate(ndimage.find_objects(x), start=1):
        print i, item
    

    This will look slightly different than you might expect. These are slice objects, so the “max” value will always be one higher than the “max” in the previous example. This is so that you can simply slice with the given tuple to get the data in question.

    E.g.

    for i, item in enumerate(ndimage.find_objects(x), start=1):
        print i, ':'
        print x[item], '\n'
    

    If you really want the starts and stops, just do something like this:

        for i, (rowslice, colslice) in enumerate(ndimage.find_objects(x), start=1):
            print i, 
            print (rowslice.start, rowslice.stop - 1),
            print (colslice.start, colslice.stop - 1)
    

    If your unique values are not sequential integers, you’ll need to do a bit of pre-processing, as I mentioned before. You might do something like this:

    import numpy as np
    import scipy.ndimage as ndimage
    
    x = np.array([[1.1, 0.0, 1.1, 0.9, 0.9],
                  [1.1, 0.0, 1.1, 0.9, 0.9],
                  [3.3, 0.0, 3.3, 4.4, 4.4],
                  [3.3, 0.0, 3.3, 4.4, 4.4]])
    ignored_val = 0.0
    labels = np.zeros(data.shape, dtype=np.int)
    
    i = 1
    for val in np.unique(x):
        if val != ignored_val:
            labels[x == val] = i
            i += 1
    
    # Now we can use the "labels" array as input to find_objects
    for i, item in enumerate(ndimage.find_objects(labels), start=1):
        print i, ':'
        print x[item], '\n'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a numpy array and I want to force every element that is
I have a NumPy array [1,2,3,4,5,6,7,8,9,10,11,12,13,14] and want to have an array structured like
I have an array a=[1,2,3,4,5,6,7,8,9] and I want to find the indices of the
I have a very long list in a numpy.array . I want to generate
I have a 2d array in numpy where I want to insert a new
I have a 2D numpy array that I want to plot in a colorbar.
I have a numpy array of dimension (48, 366, 3) and I want to
I have an numpy 2D array and I want it to return coloumn c
Assuming I have a numpy array like: [1,2,3,4,5,6] and another array: [0,0,1,2,2,1] I want
I have a NumPy array, i want to accumulate the values of one column,

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.