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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:13:11+00:00 2026-05-25T15:13:11+00:00

i have two 1D numpy arrays. The lengths are unequal. I want to make

  • 0

i have two 1D numpy arrays. The lengths are unequal. I want to make pairs (array1_elemnt,array2_element) of the elements which are close to each other. Lets consider following example

    a = [1,2,3,8,20,23]
    b = [1,2,3,5,7,21,35]

The expected result is

    [(1,1), 
    (2,2), 
    (3,3), 
    (8,7),
    (20,21),
    (23,25)]

It is important to note that 5 is left alone. It could easily be done by loops but I have very large arrays. I considered using nearest neighbor. But felt like killing a sparrow with a canon.

Can anybody please suggest any elegant solution.

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-25T15:13:11+00:00Added an answer on May 25, 2026 at 3:13 pm

    You could use the built in map function to vectorize a function that does this. For example:

    ar1 = np.array([1,2,3,8,20,23])
    ar2 = np.array([1,2,3,5,7,21,35])
    def closest(ar1, ar2, iter):
        x = np.abs(ar1[iter] - ar2)
        index = np.where(x==x.min())
        value = ar2[index]
        return value
    
    def find(x):
        return closest(ar1, ar2, x)
    c = np.array(map(find, range(ar1.shape[0])))
    

    In the example above, it looked like you wanted to exclude values once they had been paired. In that case, you could include a removal process in the first function like this, but be very careful about how array 1 is sorted:

     def closest(ar1, ar2, iter):
        x = np.abs(ar1[iter] - ar2)
        index = np.where(x==x.min())
        value = ar2[index]
        ar2[ar2==value] = -10000000
        return value
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two numpy arrays that are OpenCV convex hulls and I want to
I have two numpy arrays X and W each with shape (N,N) that result
I have two arrays in numpy. The first is a 2d array, which can
I have two ordered numpy arrays and I want to interleave them so that
I have two 2D numpy arrays - real r , which contains points in
I have two 10x2x2 complex64 arrays. I want to find which of the 2x2
I have two numpy arrays that define the x and y axes of a
In short: I have two matrices (or arrays): import numpy block_1 = numpy.matrix([[ 0,
I have two equal-length 1D numpy arrays, id and data , where id is
Suppose that I have two numpy arrays of the form x = [[1,2] [2,4]

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.