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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:18:48+00:00 2026-06-14T19:18:48+00:00

I have a matrix like A= [ 1 2 4 2 3 1 3

  • 0

I have a matrix like

A= [ 1 2 4
     2 3 1
     3 1 2 ]

and I would like to calculate its cumulative sum by row and by column, that is, I want the result to be

B = [ 1  3  7 
      3  8  13 
      6  12 19 ]

Any ideas of how to make this in R in a fast way? (Possibly using the function cumsum)
(I have huge matrices)

Thanks!

  • 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-14T19:18:49+00:00Added an answer on June 14, 2026 at 7:18 pm

    A one-liner:

    t(apply(apply(A, 2, cumsum)), 1, cumsum))
    

    The underlying observation is that you can first compute the cumulative sums over the columns and then the cumulative sum of this matrix over the rows.

    Note: When doing the rows, you have to transpose the resulting matrix.

    Your example:

    > apply(A, 2, cumsum)
         [,1] [,2] [,3]
    [1,]    1    2    4
    [2,]    3    5    5
    [3,]    6    6    7
    
    > t(apply(apply(A, 2, cumsum), 1, cumsum))
         [,1] [,2] [,3]
    [1,]    1    3    7
    [2,]    3    8   13
    [3,]    6   12   19
    

    About performance: I have now idea how good this approach scales to big matrices. Complexity-wise, this should be close to optimal. Usually, apply is not that bad in performance as well.


    Edit

    Now I was getting curious – what approach is the better one? A short benchmark:

    > A <- matrix(runif(1000*1000, 1, 500), 1000)
    > 
    > system.time(
    +   B <- t(apply(apply(A, 2, cumsum), 1, cumsum))
    + )
           User      System     elapsed 
          0.082       0.011       0.093 
    > 
    > system.time(
    +   C <- lower.tri(diag(nrow(A)), diag = TRUE) %*% A %*% upper.tri(diag(ncol(A)), diag = TRUE)
    + )
           User      System     elapsed 
          1.519       0.016       1.530 
    

    Thus: Apply outperforms matrix multiplication by a factor of 15. (Just for comparision: MATLAB needed 0.10719 seconds.) The results do not really surprise, as the apply-version can be done in O(n^2), while the matrix multiplication will need approx. O(n^2.7) computations. Thus, all optimizations that matrix multiplication offers should be lost if n is big enough.

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

Sidebar

Related Questions

I have a matrix mat and would like to calculate the mean of the
I have a matrix in R that I would like to take a single
I would like to calculate an inverse of matrix in java. Are there any
I have a matrix of one column and 6 rows. I would like to
I have matrix (a) with (1:10),<10 x 1> double. I would like to copy
I'm using Numpy and have a 7x12x12 matrix whose values I would like to
Suppose that I have a 2D array (matrix) in Java like this... int[][] MyMat
I have a vector, v3, that looks like this: v3 <- matrix(c(1,1,3,3,2,2, 2,3,1,2,3,1, 3,2,2,1,1,3),
I have a numeric data matrix that looks like below: (Matrix) Sample1 Sample2 Sample3
As per Row wise matrix operations in R I would like to apply a

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.