Cumulative total of columns in a matrix or table

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
4
down vote

favorite












I have the following:



matrix1 = a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p


(Note that it's the data that matters, not the fact it's defined as a matrix. Could be a table, could be a list.)



I want to create a cumulative sum, so that each entry is the sum of all data in that row up to and including column n, which is the column where the sum is to be placed. In other words, I want



matrix2 = a, a + b, a + b + c, a + b + c + d, e, e + f, e + f + g, e + f + g + h, i, i + j, i + j + k, i + j + k + l, m, m + n, 
m + n + o, m + n + o + p


I assume there must be a way to do this using Total[matrix1 ,2] combined with some way to specify the number of columns to sum - maybe Range...? Or should I be using Part and Span? If so, how?



Can anyone help with this? Thanks in advance.










share|improve this question

























    up vote
    4
    down vote

    favorite












    I have the following:



    matrix1 = a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p


    (Note that it's the data that matters, not the fact it's defined as a matrix. Could be a table, could be a list.)



    I want to create a cumulative sum, so that each entry is the sum of all data in that row up to and including column n, which is the column where the sum is to be placed. In other words, I want



    matrix2 = a, a + b, a + b + c, a + b + c + d, e, e + f, e + f + g, e + f + g + h, i, i + j, i + j + k, i + j + k + l, m, m + n, 
    m + n + o, m + n + o + p


    I assume there must be a way to do this using Total[matrix1 ,2] combined with some way to specify the number of columns to sum - maybe Range...? Or should I be using Part and Span? If so, how?



    Can anyone help with this? Thanks in advance.










    share|improve this question























      up vote
      4
      down vote

      favorite









      up vote
      4
      down vote

      favorite











      I have the following:



      matrix1 = a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p


      (Note that it's the data that matters, not the fact it's defined as a matrix. Could be a table, could be a list.)



      I want to create a cumulative sum, so that each entry is the sum of all data in that row up to and including column n, which is the column where the sum is to be placed. In other words, I want



      matrix2 = a, a + b, a + b + c, a + b + c + d, e, e + f, e + f + g, e + f + g + h, i, i + j, i + j + k, i + j + k + l, m, m + n, 
      m + n + o, m + n + o + p


      I assume there must be a way to do this using Total[matrix1 ,2] combined with some way to specify the number of columns to sum - maybe Range...? Or should I be using Part and Span? If so, how?



      Can anyone help with this? Thanks in advance.










      share|improve this question













      I have the following:



      matrix1 = a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p


      (Note that it's the data that matters, not the fact it's defined as a matrix. Could be a table, could be a list.)



      I want to create a cumulative sum, so that each entry is the sum of all data in that row up to and including column n, which is the column where the sum is to be placed. In other words, I want



      matrix2 = a, a + b, a + b + c, a + b + c + d, e, e + f, e + f + g, e + f + g + h, i, i + j, i + j + k, i + j + k + l, m, m + n, 
      m + n + o, m + n + o + p


      I assume there must be a way to do this using Total[matrix1 ,2] combined with some way to specify the number of columns to sum - maybe Range...? Or should I be using Part and Span? If so, how?



      Can anyone help with this? Thanks in advance.







      list-manipulation matrix table summation






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 31 at 6:34









      Richard Burke-Ward

      3839




      3839




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          I first came up with:



          FoldList[Plus, #[[1]], Rest[#]] & /@ matrix


          Then read the documentation for Accumulate, which says it is equivalent to:



          Rest[FoldList[Plus, 0, #]] & /@ matrix1


          One nice thing about these approaches is they generalize beyond sums:



          FoldList[Times, #[[1]], Rest[#]] & /@ matrix1
          Rest[FoldList[Times, 1, #]] & /@ matrix1





          share|improve this answer



























            up vote
            3
            down vote













            Just for fun, another approach using matrix-matrix multiplication:



            acc = matrix1.UpperTriangularize[ConstantArray[1, 1, 1 Dimensions[matrix1][[2]]]];
            acc // MatrixForm



            $left(
            beginarraycccc
            a & a+b & a+b+c & a+b+c+d \
            e & e+f & e+f+g & e+f+g+h \
            i & i+j & i+j+k & i+j+k+l \
            m & m+n & m+n+o & m+n+o+p \
            endarray
            right)$




            As alternative to Accumulate[matrix1], you could use



            acc = LowerTriangularize[ConstantArray[1, 1, 1 Dimensions[matrix1[[1]]]].matrix1;
            acc // MatrixForm



            $left(
            beginarraycccc
            a & b & c & d \
            a+e & b+f & c+g & d+h \
            a+e+i & b+f+j & c+g+k & d+h+l \
            a+e+i+m & b+f+j+n & c+g+k+o & d+h+l+p \
            endarray
            right)$




            I would not advise to use these in practice as they have comptational complexity $O(n^3)$ while the methods involving Accumulate have only complexity $O(n^2)$.






            share|improve this answer




















            • Dear @Richard, you can only mark a single answer as the answer to your question (although you can upvote as many as you like). In this case, I would recommend to mark kglr's post as answer as it is (i) more concise and (ii) more efficient than mine.
              – Henrik Schumacher
              Aug 31 at 9:53











            • Clever. Accumulate is probably the standard way, but I do prefer/like Linear Algebra solutions.
              – Anton Antonov
              Aug 31 at 12:43










            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "387"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f180956%2fcumulative-total-of-columns-in-a-matrix-or-table%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            4
            down vote



            accepted










            I first came up with:



            FoldList[Plus, #[[1]], Rest[#]] & /@ matrix


            Then read the documentation for Accumulate, which says it is equivalent to:



            Rest[FoldList[Plus, 0, #]] & /@ matrix1


            One nice thing about these approaches is they generalize beyond sums:



            FoldList[Times, #[[1]], Rest[#]] & /@ matrix1
            Rest[FoldList[Times, 1, #]] & /@ matrix1





            share|improve this answer
























              up vote
              4
              down vote



              accepted










              I first came up with:



              FoldList[Plus, #[[1]], Rest[#]] & /@ matrix


              Then read the documentation for Accumulate, which says it is equivalent to:



              Rest[FoldList[Plus, 0, #]] & /@ matrix1


              One nice thing about these approaches is they generalize beyond sums:



              FoldList[Times, #[[1]], Rest[#]] & /@ matrix1
              Rest[FoldList[Times, 1, #]] & /@ matrix1





              share|improve this answer






















                up vote
                4
                down vote



                accepted







                up vote
                4
                down vote



                accepted






                I first came up with:



                FoldList[Plus, #[[1]], Rest[#]] & /@ matrix


                Then read the documentation for Accumulate, which says it is equivalent to:



                Rest[FoldList[Plus, 0, #]] & /@ matrix1


                One nice thing about these approaches is they generalize beyond sums:



                FoldList[Times, #[[1]], Rest[#]] & /@ matrix1
                Rest[FoldList[Times, 1, #]] & /@ matrix1





                share|improve this answer












                I first came up with:



                FoldList[Plus, #[[1]], Rest[#]] & /@ matrix


                Then read the documentation for Accumulate, which says it is equivalent to:



                Rest[FoldList[Plus, 0, #]] & /@ matrix1


                One nice thing about these approaches is they generalize beyond sums:



                FoldList[Times, #[[1]], Rest[#]] & /@ matrix1
                Rest[FoldList[Times, 1, #]] & /@ matrix1






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Aug 31 at 11:17









                Lee

                40817




                40817




















                    up vote
                    3
                    down vote













                    Just for fun, another approach using matrix-matrix multiplication:



                    acc = matrix1.UpperTriangularize[ConstantArray[1, 1, 1 Dimensions[matrix1][[2]]]];
                    acc // MatrixForm



                    $left(
                    beginarraycccc
                    a & a+b & a+b+c & a+b+c+d \
                    e & e+f & e+f+g & e+f+g+h \
                    i & i+j & i+j+k & i+j+k+l \
                    m & m+n & m+n+o & m+n+o+p \
                    endarray
                    right)$




                    As alternative to Accumulate[matrix1], you could use



                    acc = LowerTriangularize[ConstantArray[1, 1, 1 Dimensions[matrix1[[1]]]].matrix1;
                    acc // MatrixForm



                    $left(
                    beginarraycccc
                    a & b & c & d \
                    a+e & b+f & c+g & d+h \
                    a+e+i & b+f+j & c+g+k & d+h+l \
                    a+e+i+m & b+f+j+n & c+g+k+o & d+h+l+p \
                    endarray
                    right)$




                    I would not advise to use these in practice as they have comptational complexity $O(n^3)$ while the methods involving Accumulate have only complexity $O(n^2)$.






                    share|improve this answer




















                    • Dear @Richard, you can only mark a single answer as the answer to your question (although you can upvote as many as you like). In this case, I would recommend to mark kglr's post as answer as it is (i) more concise and (ii) more efficient than mine.
                      – Henrik Schumacher
                      Aug 31 at 9:53











                    • Clever. Accumulate is probably the standard way, but I do prefer/like Linear Algebra solutions.
                      – Anton Antonov
                      Aug 31 at 12:43














                    up vote
                    3
                    down vote













                    Just for fun, another approach using matrix-matrix multiplication:



                    acc = matrix1.UpperTriangularize[ConstantArray[1, 1, 1 Dimensions[matrix1][[2]]]];
                    acc // MatrixForm



                    $left(
                    beginarraycccc
                    a & a+b & a+b+c & a+b+c+d \
                    e & e+f & e+f+g & e+f+g+h \
                    i & i+j & i+j+k & i+j+k+l \
                    m & m+n & m+n+o & m+n+o+p \
                    endarray
                    right)$




                    As alternative to Accumulate[matrix1], you could use



                    acc = LowerTriangularize[ConstantArray[1, 1, 1 Dimensions[matrix1[[1]]]].matrix1;
                    acc // MatrixForm



                    $left(
                    beginarraycccc
                    a & b & c & d \
                    a+e & b+f & c+g & d+h \
                    a+e+i & b+f+j & c+g+k & d+h+l \
                    a+e+i+m & b+f+j+n & c+g+k+o & d+h+l+p \
                    endarray
                    right)$




                    I would not advise to use these in practice as they have comptational complexity $O(n^3)$ while the methods involving Accumulate have only complexity $O(n^2)$.






                    share|improve this answer




















                    • Dear @Richard, you can only mark a single answer as the answer to your question (although you can upvote as many as you like). In this case, I would recommend to mark kglr's post as answer as it is (i) more concise and (ii) more efficient than mine.
                      – Henrik Schumacher
                      Aug 31 at 9:53











                    • Clever. Accumulate is probably the standard way, but I do prefer/like Linear Algebra solutions.
                      – Anton Antonov
                      Aug 31 at 12:43












                    up vote
                    3
                    down vote










                    up vote
                    3
                    down vote









                    Just for fun, another approach using matrix-matrix multiplication:



                    acc = matrix1.UpperTriangularize[ConstantArray[1, 1, 1 Dimensions[matrix1][[2]]]];
                    acc // MatrixForm



                    $left(
                    beginarraycccc
                    a & a+b & a+b+c & a+b+c+d \
                    e & e+f & e+f+g & e+f+g+h \
                    i & i+j & i+j+k & i+j+k+l \
                    m & m+n & m+n+o & m+n+o+p \
                    endarray
                    right)$




                    As alternative to Accumulate[matrix1], you could use



                    acc = LowerTriangularize[ConstantArray[1, 1, 1 Dimensions[matrix1[[1]]]].matrix1;
                    acc // MatrixForm



                    $left(
                    beginarraycccc
                    a & b & c & d \
                    a+e & b+f & c+g & d+h \
                    a+e+i & b+f+j & c+g+k & d+h+l \
                    a+e+i+m & b+f+j+n & c+g+k+o & d+h+l+p \
                    endarray
                    right)$




                    I would not advise to use these in practice as they have comptational complexity $O(n^3)$ while the methods involving Accumulate have only complexity $O(n^2)$.






                    share|improve this answer












                    Just for fun, another approach using matrix-matrix multiplication:



                    acc = matrix1.UpperTriangularize[ConstantArray[1, 1, 1 Dimensions[matrix1][[2]]]];
                    acc // MatrixForm



                    $left(
                    beginarraycccc
                    a & a+b & a+b+c & a+b+c+d \
                    e & e+f & e+f+g & e+f+g+h \
                    i & i+j & i+j+k & i+j+k+l \
                    m & m+n & m+n+o & m+n+o+p \
                    endarray
                    right)$




                    As alternative to Accumulate[matrix1], you could use



                    acc = LowerTriangularize[ConstantArray[1, 1, 1 Dimensions[matrix1[[1]]]].matrix1;
                    acc // MatrixForm



                    $left(
                    beginarraycccc
                    a & b & c & d \
                    a+e & b+f & c+g & d+h \
                    a+e+i & b+f+j & c+g+k & d+h+l \
                    a+e+i+m & b+f+j+n & c+g+k+o & d+h+l+p \
                    endarray
                    right)$




                    I would not advise to use these in practice as they have comptational complexity $O(n^3)$ while the methods involving Accumulate have only complexity $O(n^2)$.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Aug 31 at 6:58









                    Henrik Schumacher

                    39.3k254117




                    39.3k254117











                    • Dear @Richard, you can only mark a single answer as the answer to your question (although you can upvote as many as you like). In this case, I would recommend to mark kglr's post as answer as it is (i) more concise and (ii) more efficient than mine.
                      – Henrik Schumacher
                      Aug 31 at 9:53











                    • Clever. Accumulate is probably the standard way, but I do prefer/like Linear Algebra solutions.
                      – Anton Antonov
                      Aug 31 at 12:43
















                    • Dear @Richard, you can only mark a single answer as the answer to your question (although you can upvote as many as you like). In this case, I would recommend to mark kglr's post as answer as it is (i) more concise and (ii) more efficient than mine.
                      – Henrik Schumacher
                      Aug 31 at 9:53











                    • Clever. Accumulate is probably the standard way, but I do prefer/like Linear Algebra solutions.
                      – Anton Antonov
                      Aug 31 at 12:43















                    Dear @Richard, you can only mark a single answer as the answer to your question (although you can upvote as many as you like). In this case, I would recommend to mark kglr's post as answer as it is (i) more concise and (ii) more efficient than mine.
                    – Henrik Schumacher
                    Aug 31 at 9:53





                    Dear @Richard, you can only mark a single answer as the answer to your question (although you can upvote as many as you like). In this case, I would recommend to mark kglr's post as answer as it is (i) more concise and (ii) more efficient than mine.
                    – Henrik Schumacher
                    Aug 31 at 9:53













                    Clever. Accumulate is probably the standard way, but I do prefer/like Linear Algebra solutions.
                    – Anton Antonov
                    Aug 31 at 12:43




                    Clever. Accumulate is probably the standard way, but I do prefer/like Linear Algebra solutions.
                    – Anton Antonov
                    Aug 31 at 12:43

















                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f180956%2fcumulative-total-of-columns-in-a-matrix-or-table%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Bahrain

                    Postfix configuration issue with fips on centos 7; mailgun relay