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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:32:56+00:00 2026-06-07T19:32:56+00:00

I have an interesting puzzle. Suppose you have a numpy 2D array, in which

  • 0

I have an interesting puzzle. Suppose you have a numpy 2D array, in which each line corresponds to a measurement event and each column corresponds to different measured variable. One additional column in this array specifies the date at which the measurement was taken. The lines are sorted according to the time stamp. There are several (or many) measurements on each day. The goal is to identify the lines that correspond to a new day and subtract the respective values from the subsequent lines in that day.

I approach this problem by a loop that loops over the days, creating a boolean vector that selects the proper lines and then subtracting the first selected line. This approach works, but feels non-elegant. Are there better ways to do this?

Just a small example. The lines below define a matrix in which the first colum
is the day and the remaining two are the measured values

before = array([[ 1,  1,  2],
   [ 1,  3,  4],
   [ 1,  5,  6],
   [ 2,  7,  8],
   [ 3,  9, 10],
   [ 3, 11, 12],
   [ 3, 13, 14]])

at the end of the process I expect to see the following array:

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

PS Please help me finding a better and more informative title for this post. I’m out of ideas

  • 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-07T19:32:58+00:00Added an answer on June 7, 2026 at 7:32 pm

    numpy.searchsorted is a convenient function for this:

    In : before
    Out:
    array([[ 1,  1,  2],
           [ 1,  3,  4],
           [ 1,  5,  6],
           [ 2,  7,  8],
           [ 3,  9, 10],
           [ 3, 11, 12],
           [ 3, 13, 14]])
    
    In : diff = before[before[:,0].searchsorted(x[:,0])]
    
    In : diff[:,0] = 0
    
    In : before - diff
    Out:
    array([[1, 0, 0],
           [1, 2, 2],
           [1, 4, 4],
           [2, 0, 0],
           [3, 0, 0],
           [3, 2, 2],
           [3, 4, 4]])
    

    Longer explanation

    If you take the first column, and search for itself you get the minimum indices for those particular values:

    In : before
    Out:
    array([[ 1,  1,  2],
           [ 1,  3,  4],
           [ 1,  5,  6],
           [ 2,  7,  8],
           [ 3,  9, 10],
           [ 3, 11, 12],
           [ 3, 13, 14]])
    
    In : before[:,0].searchsorted(x[:,0])
    Out: array([0, 0, 0, 3, 4, 4, 4])
    

    You can then use this to construct the matrix that you will subtract by indexing:

    In : diff = before[before[:,0].searchsorted(x[:,0])]
    
    In : diff
    Out:
    array([[ 1,  1,  2],
           [ 1,  1,  2],
           [ 1,  1,  2],
           [ 2,  7,  8],
           [ 3,  9, 10],
           [ 3,  9, 10],
           [ 3,  9, 10]])
    

    You need to make the first column 0 so that they won’t be subtracted.

    In : diff[:,0] = 0
    
    In : diff
    Out:
    array([[ 0,  1,  2],
           [ 0,  1,  2],
           [ 0,  1,  2],
           [ 0,  7,  8],
           [ 0,  9, 10],
           [ 0,  9, 10],
           [ 0,  9, 10]])
    

    Finally, subtract two matrices to get the desired output:

    In : before - diff
    Out:
    array([[1, 0, 0],
           [1, 2, 2],
           [1, 4, 4],
           [2, 0, 0],
           [3, 0, 0],
           [3, 2, 2],
           [3, 4, 4]])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an interesting scenario. I have a task model which has a task
I have an interesting dilemma to which, I hope, someone may have an answer.
I have an interesting programming puzzle for you: You will be given two things:
I have an interesting problem. I have a page containing a Telerik chart which
I have an interesting problem. I define, typedef char *string; char array[10]; string buf[10];
I have an interesting situation - I have a table row which, currently, shows
I have an interesting scenario. I have a custom TableViewCell class which contains a
I have an interesting problem. I pull down an android project from cvs, which
I have an interesting bitmask puzzle problem I need help solving in something. Here
I have interesting task which requires me to download a dynamically generated file from

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.