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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:01:54+00:00 2026-06-13T21:01:54+00:00

First time posting a question here! Will happily take any advice || criticism I

  • 0

First time posting a question here! Will happily take any advice||criticism I can get.

We have two vectors: v1 and v2. Assume length(v1) >> length(v2). I move a window of size length(v2) along the vector v1. At each lag index, from the windowed portion of v1 I subtract v2. I then sum the terms of the resulting vector and return this sum for every lag index along the length of vector v1. For simplicity, let’s ignore the edge cases.

I’ve done this with a for loop, but the length of my vectors is on the order of 10^9 [and larger], and even though the calculation is simple, it appears to take a long time to just iterate through the whole thing.

Any ideas? I suspect there is a function that does something like this, but I have had no luck finding it.

  • 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-13T21:01:55+00:00Added an answer on June 13, 2026 at 9:01 pm

    If you’ll allow me to reframe the question, you basically want a windowed sum of one vector. You’re then subtracting from this the sum of some other vector.

    My solution would be to use the cumulative sum function in matlab (cumsum) as follows:

    l1 = length(v1);
    l2 = length(v2);
    windowSum = cumsum( [v1 zeros(1,l2)] ) - cumsum( [zeros(1,l2) v1] );
    returnVal = windowSum - sum(v2);
    

    This assumes that v1 and v2 are row-vectors. It pads v1 with zeros on the right, takes the cumulative sum, then subtracts the cumulative sum with the zeros padded to the left. This results in a vector such that windowSum(i) is the sum of the length(v2) elements of v1 preceeding index i. Depending on how you want the edges handled, what you want the length of the vector to be, etc., this should solve your problem.

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

Sidebar

Related Questions

this is my first time posting here, I have a question which I have
First time posting here, will try to be succinct. This is a classic 'can't
first time posting here so be gentle :) On my web pages I have
this is my first time posting on here and have read alot of helpful
This is my first time posting a question here, it has been a valuable
this is my first time posting a question here - I've searched for ones
My first time posting a question here so please do not mind my mistakes
first time posting here. I'm using a loop to create 3 buttons, but my
my first time posting. I have a tricky task of finding the latest date
This is the first time I am posting a question on stackoverflow, so please

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.