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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:37:47+00:00 2026-06-14T23:37:47+00:00

I found dozens of examples how to vectorize for loops in Python/NumPy. Unfortunately, I

  • 0

I found dozens of examples how to vectorize for loops in Python/NumPy. Unfortunately, I don’t get how I can reduce the computation time of my simple for loop using a vectorized form. Is it even possible in this case?

time = np.zeros(185000)
lat1 = np.array(([48.78,47.45],[38.56,39.53],...)) # ~ 200000 rows
lat2 = np.array(([7.78,5.45],[7.56,5.53],...)) # same number of rows as time
for ii in np.arange(len(time)):
    pos = np.argwhere( (lat1[:,0]==lat2[ii,0]) and \
                       (lat1[:,1]==lat2[ii,1]) )
    if pos.size:
        pos = int(pos)
        time[ii] = dtime[pos]
  • 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-14T23:37:49+00:00Added an answer on June 14, 2026 at 11:37 pm

    Probably the fastest way to find all matches is to sort both arrays and walk through them together, like this working example:

    import numpy as np
    
    def is_less(a, b):
        # this ugliness is needed because we want to compare lexicographically same as np.lexsort(), from the last column backward
        for i in range(len(a)-1, -1, -1):
            if a[i]<b[i]: return True
            elif a[i]>b[i]: return False
        return False
    
    def is_equal(a, b):
        for i in range(len(a)):
            if a[i] != b[i]: return False
        return True
    
    # lat1 = np.array(([48.78,47.45],[38.56,39.53]))
    # lat2 = np.array(([7.78,5.45],[48.78,47.45],[7.56,5.53]))
    lat1 = np.load('arr.npy')
    lat2 = np.load('refarr.npy')
    
    idx1 = np.lexsort( lat1.transpose() )
    idx2 = np.lexsort( lat2.transpose() )
    ii = 0
    jj = 0
    while ii < len(idx1) and jj < len(idx2):
        a = lat1[ idx1[ii] , : ]
        b = lat2[ idx2[jj] , : ]
        if is_equal( a, b ):
            # do stuff with match
            print "match found: lat1=%s lat2=%s %d and %d" % ( repr(a), repr(b), idx1[ii], idx2[jj] )
            ii += 1
            jj += 1
        elif is_less( a, b ):
            ii += 1
        else:
            jj += 1
    

    This may not be perfectly pythonic (perhaps someone can think of a nicer implementation using generators or itertools?) but it is hard to imagine any method that relies on searching one point at a time beating this in speed.

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

Sidebar

Related Questions

FOUND THE ANSWER MYSELF, see below... I'd like to have a very simple user
found this regex: insert every 10 characters: $text = preg_replace(|(.{10})|u, \${1}. , $text); can
I'm using appengine servers. I expect to get many requests (dozens) in close proximity
Subj. I mean that I don't want to paste dozens of '\' into search
From my searches online, I've found dozens of web-based MongoDB GUIs, as well as
I have looked through dozens and dozens of sliders but can't find the one
I've had it of those dozens of editors which never provides simple things like:
The ClassNotFoundException questions are found by the dozens on SO but even so I
Have worked in dozens of languages but new to Python. My first (maybe second)
Found this while reading the Neo4j manual, specifically here , I found the sentence:

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.