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

  • Home
  • SEARCH
  • 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 8093661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:32:58+00:00 2026-06-05T20:32:58+00:00

I have a three dimensional ndarray of 2D coordinates, for example: [[[1704 1240] [1745

  • 0

I have a three dimensional ndarray of 2D coordinates, for example:

[[[1704 1240]
  [1745 1244]
  [1972 1290]
  [2129 1395]
  [1989 1332]]

 [[1712 1246]
  [1750 1246]
  [1964 1286]
  [2138 1399]
  [1989 1333]]

 [[1721 1249]
  [1756 1249]
  [1955 1283]
  [2145 1399]
  [1990 1333]]]

The ultimate goal is to remove the point closest to a given point ([1989 1332]) from each “group” of 5 coordinates. My thought was to produce a similarly shaped array of distances, and then using argmin to determine the indices of the values to be removed. However, I am not certain how to go about applying a function, like one to calculate a distance to a given point, to every element in an ndarray, at least in a NumPythonic way.

  • 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-06-05T20:33:00+00:00Added an answer on June 5, 2026 at 8:33 pm

    List comprehensions are a very inefficient way to deal with numpy arrays. They’re an especially poor choice for the distance calculation.

    To find the difference between your data and a point, you’d just do data - point. You can then calculate the distance using np.hypot, or if you’d prefer, square it, sum it, and take the square root.

    It’s a bit easier if you make it an Nx2 array for the purposes of the calculation though.

    Basically, you want something like this:

    import numpy as np
    
    data = np.array([[[1704, 1240],
                      [1745, 1244],
                      [1972, 1290],
                      [2129, 1395],
                      [1989, 1332]],
    
                     [[1712, 1246],
                      [1750, 1246],
                      [1964, 1286],
                      [2138, 1399],
                      [1989, 1333]],
    
                     [[1721, 1249],
                      [1756, 1249],
                      [1955, 1283],
                      [2145, 1399],
                      [1990, 1333]]])
    
    point = [1989, 1332]
    
    #-- Calculate distance ------------
    # The reshape is to make it a single, Nx2 array to make calling `hypot` easier
    dist = data.reshape((-1,2)) - point
    dist = np.hypot(*dist.T)
    
    # We can then reshape it back to AxBx1 array, similar to the original shape
    dist = dist.reshape(data.shape[0], data.shape[1], 1)
    print dist
    

    This yields:

    array([[[ 299.48121811],
            [ 259.38388539],
            [  45.31004304],
            [ 153.5219854 ],
            [   0.        ]],
    
           [[ 290.04310025],
            [ 254.0019685 ],
            [  52.35456045],
            [ 163.37074401],
            [   1.        ]],
    
           [[ 280.55837182],
            [ 247.34186868],
            [  59.6405902 ],
            [ 169.77926846],
            [   1.41421356]]])
    

    Now, removing the closest element is a bit harder than simply getting the closest element.

    With numpy, you can use boolean indexing to do this fairly easily.

    However, you’ll need to worry a bit about the alignment of your axes.

    The key is to understand that numpy “broadcasts” operations along the last axis. In this case, we want to brodcast along the middle axis.

    Also, -1 can be used as a placeholder for the size of an axis. Numpy will calculate the permissible size when -1 is put in as the size of an axis.

    What we’d need to do would look a bit like this:

    #-- Remove closest point ---------------------
    mask = np.squeeze(dist) != dist.min(axis=1)
    filtered = data[mask]
    
    # Once again, let's reshape things back to the original shape...
    filtered = filtered.reshape(data.shape[0], -1, data.shape[2])
    

    You could make that a single line, I’m just breaking it down for readability. The key is that dist != something yields a boolean array which you can then use to index the original array.

    So, Putting it all together:

    import numpy as np
    
    data = np.array([[[1704, 1240],
                      [1745, 1244],
                      [1972, 1290],
                      [2129, 1395],
                      [1989, 1332]],
    
                     [[1712, 1246],
                      [1750, 1246],
                      [1964, 1286],
                      [2138, 1399],
                      [1989, 1333]],
    
                     [[1721, 1249],
                      [1756, 1249],
                      [1955, 1283],
                      [2145, 1399],
                      [1990, 1333]]])
    
    point = [1989, 1332]
    
    #-- Calculate distance ------------
    # The reshape is to make it a single, Nx2 array to make calling `hypot` easier
    dist = data.reshape((-1,2)) - point
    dist = np.hypot(*dist.T)
    
    # We can then reshape it back to AxBx1 array, similar to the original shape
    dist = dist.reshape(data.shape[0], data.shape[1], 1)
    
    #-- Remove closest point ---------------------
    mask = np.squeeze(dist) != dist.min(axis=1)
    filtered = data[mask]
    
    # Once again, let's reshape things back to the original shape...
    filtered = filtered.reshape(data.shape[0], -1, data.shape[2])
    
    print filtered
    

    Yields:

    array([[[1704, 1240],
            [1745, 1244],
            [1972, 1290],
            [2129, 1395]],
    
           [[1712, 1246],
            [1750, 1246],
            [1964, 1286],
            [2138, 1399]],
    
           [[1721, 1249],
            [1756, 1249],
            [1955, 1283],
            [2145, 1399]]])
    

    On a side note, if more than one point is equally close, this won’t work. Numpy arrays have to have the same number of elements along each dimension, so you’ll need to re-do your grouping in that case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a three dimensional bit table array as bit_table[dim1][100][200]; The second and third
i am using visual studio 2010. i have a three dimensional structure array which,
I have a dataset consisting of a large collection of points in three dimensional
I have the following three arrays and need to create a new two-dimensional array
I have X , a three-dimensional array in R. I want to take a
I have three one-dimensional arrays. The task is to store the numbers which exist
I have an application with a Canvas3D class for drawing three-dimensional objects. The canvas
I have two vectors defining two separate points in a three-dimensional space. One is
I have three dimensional array of data that is generated from web server logs
i have a three dimensional array which i am using as a bit table

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.