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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:26:34+00:00 2026-06-09T04:26:34+00:00

I’m trying to find the fastest way to find the first non-zero value for

  • 0

I’m trying to find the fastest way to find the first non-zero value for each row of a two dimensional sorted array. Technically, the only values in the array are zeros and ones, and it is “sorted”.

For instance, the array could look like the following:

v =

0 0 0 1 1 1 1 
0 0 0 1 1 1 1 
0 0 0 0 1 1 1 
0 0 0 0 0 0 1 
0 0 0 0 0 0 1 
0 0 0 0 0 0 1 
0 0 0 0 0 0 0

I could use the argmax function

argmax(v, axis=1))

to find when it changes from zero to one, but I believe this would do an exhaustive search along each row. My array will be reasonably sized (~2000×2000). Would argmax still outperform just doing a searchsorted approach for each row within a for loop, or is there a better alternative?

Also, the array will always be such that the first position of a one for a row is always >= the first position of a one in the row above it (but it is not guaranteed that there will be a one in the last few rows). I could exploit this with a for loop and a “starting index value” for each row equal to the position of the first 1 from the previous row, but am i correct in thinking that the numpy argmax function will still outperform a loop written in python.

I would just benchmark the alternatives, but the edge length of the array could change quite a bit (from 250 to 10,000).

  • 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-09T04:26:36+00:00Added an answer on June 9, 2026 at 4:26 am

    It is reasonably fast to use np.where:

    >>> a
    array([[0, 0, 0, 1, 1, 1, 1],
           [0, 0, 0, 1, 1, 1, 1],
           [0, 0, 0, 0, 1, 1, 1],
           [0, 0, 0, 0, 0, 0, 1],
           [0, 0, 0, 0, 0, 0, 1],
           [0, 0, 0, 0, 0, 0, 1],
           [0, 0, 0, 0, 0, 0, 0]])
    >>> np.where(a>0)
    (array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 3, 4, 5]), array([3, 4, 5, 6, 3, 4, 5, 6, 4, 5, 6, 6, 6, 6]))
    

    That delivers tuples with to coordinates of the values greater than 0.

    You can also use np.where to test each sub array:

    def first_true1(a):
        """ return a dict of row: index with value in row > 0 """
        di={}
        for i in range(len(a)):
            idx=np.where(a[i]>0)
            try:
                di[i]=idx[0][0]
            except IndexError:
                di[i]=None    
    
        return di       
    

    Prints:

    {0: 3, 1: 3, 2: 4, 3: 6, 4: 6, 5: 6, 6: None}
    

    ie, row 0: index 3>0; row 4: index 4>0; row 6: no index greater than 0

    As you suspect, argmax may be faster:

    def first_true2():
        di={}
        for i in range(len(a)):
            idx=np.argmax(a[i])
            if idx>0:
                di[i]=idx
            else:
                di[i]=None    
    
        return di       
        # same dict is returned...
    

    If you can deal with the logic of not having a None for rows of all naughts, this is faster still:

    def first_true3():
        di={}
        for i, j in zip(*np.where(a>0)):
            if i in di:
                continue
            else:
                di[i]=j
    
        return di      
    

    And here is a version that uses axis in argmax (as suggested in your comments):

    def first_true4():
        di={}
        for i, ele in enumerate(np.argmax(a,axis=1)):
            if ele==0 and a[i][0]==0:
                di[i]=None
            else:
                di[i]=ele
    
        return di          
    

    For speed comparisons (on your example array), I get this:

                rate/sec usec/pass first_true1 first_true2 first_true3 first_true4
    first_true1   23,818    41.986          --      -34.5%      -63.1%      -70.0%
    first_true2   36,377    27.490       52.7%          --      -43.6%      -54.1%
    first_true3   64,528    15.497      170.9%       77.4%          --      -18.6%
    first_true4   79,287    12.612      232.9%      118.0%       22.9%          --
    

    If I scale that to a 2000 X 2000 np array, here is what I get:

                rate/sec  usec/pass first_true3 first_true1 first_true2 first_true4
    first_true3        3 354380.107          --       -0.3%      -74.7%      -87.8%
    first_true1        3 353327.084        0.3%          --      -74.6%      -87.7%
    first_true2       11  89754.200      294.8%      293.7%          --      -51.7%
    first_true4       23  43306.494      718.3%      715.9%      107.3%          --
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to select an H1 element which is the second-child in its group
I have an array which has BIG numbers and small numbers in it. I
I've tracked down a weird MySQL problem to the two different ways I was
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.