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

  • Home
  • SEARCH
  • 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 6760625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:05:11+00:00 2026-05-26T14:05:11+00:00

I have a long vector x, and another v, which contains lengths. I would

  • 0

I have a long vector x, and another v, which contains lengths. I would like to sum x so that the answer y is a vector of length length(v), and y[1] is sum(x[1:v[i]]), y[2] is sum(x[(1+v[1]):(v[1]+v[2])]), and so on. Essentially this is performing sparse matrix multiplication from a space of dimension length(x) to one of dimension length(v). However, I would prefer not to bring in “advanced machinery”, although I might have to. It does need to be very, very fast. Can anyone think of anything simpler than using a sparse matrix package?

Example –

x <- c(1,1,3,4,5)
v <- c(2,3)
y <- myFunc(x,v)

y should be c(2,12)

I am open to any pre-processing – e.g, storing in v the starting indexes of each stretch.

  • 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-26T14:05:11+00:00Added an answer on May 26, 2026 at 2:05 pm
      y <- cumsum(x)[cumsum(v)]
      y <- c(y[1], diff(y))
    

    This looks like it’s doing extra work because it’s computing the cumsum for the whole vector, but it’s actually faster than the other solutions so far, for both small and large numbers of groups.

    Here’s how I simulated the data

    set.seed(5)
    N <- 1e6
    n <- 10
    x <- round(runif(N,0,100),1)
    v <- as.vector(table(sample(n, N, replace=TRUE)))
    

    On my machine the timings with n <- 10 are:

    • Brandon Bertelsen (for loop): 0.017
    • Ramnath (rowsum): 0.057
    • John (split/apply): 0.280
    • Aaron (cumsum): 0.008

    changing to n <- 1e5 the timings are:

    • Brandon Bertelsen (for loop): 2.181
    • Ramnath (rowsum): 0.226
    • John (split/apply): 0.852
    • Aaron (cumsum): 0.015

    I suspect this is faster than doing matrix multiplication, even with a sparse matrix package, because one doesn’t have to form the matrix or do any multiplication. If more speed is needed, I suspect it could be sped up by writing it in C; not hard to do with the inline and rcpp packages, but I’ll leave that to you.

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

Sidebar

Related Questions

I have long been of the opinion that the time and resources it would
I have with two fields: long nr and a vector of long like below:
If I have a vector c(1,2,3) and another vector of the same length c(1,4,1)
I have long tables generated by datagrid control that go beyond the page width.
I have long recognized that any set of whitespace in an HTML file will
One thing with which I have long had problems, within the CakePHP framework, is
I'm trying to find all data items that have long/lats that are contained by
This is the first part of a function I have that's causing my program
I have a Vector of Object[] type that stores my data. How to print
I am trying to create a vector that contains pointers, each pointer points to

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.