Applying a recursive function to a list to get another list

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











up vote
2
down vote

favorite












Here is code:



x2=1539.91, 5.05, -2.82, 0, 19, 135.93, 117.78, 11.61, 8.17, 13.76, 
1.5, 36.75, 137.77, -16.18, 4.18, -2.82, 0, 18.42, 53.19, 5.91,
-16.18, 3.24, -2.82, 0, 53.19, 518.6, -16.18, 1.61, -2.82, 23, 0,
70.92, 58.89, 13.08, 42.32, 57.67, -15.32, 1.76, -2.68, 18.42, 0,
53.19, 6.33, -15.32, 2.01, -2.68, 0, 53.19, -15.32, 2.17, -2.68, 0,
-1000, 76.83, 27.18, 0.02, 8.88, 13.08, 30, 48.72, 16.02, -15.32,
1.69, -2.68, 0, 0.7, 53.19, 1128.85, 11.49, 53.19, 16.61, 209.84,
1243.2, 23, 1.08


I want to create a new list called balancelist using a recursive function (or any other way so I learn) such that I end up with a list with something like this:



1539.91,1539.91-5.05,1539.91-5.05-(-2.82),...... 


I have tried the following code but it doesn't work:



balancefinal = 
For[i = 1, i <= Length[x2], i++, x2[[i]] - x2[[i + 1]];
Append[balancefinal]]









share|improve this question



























    up vote
    2
    down vote

    favorite












    Here is code:



    x2=1539.91, 5.05, -2.82, 0, 19, 135.93, 117.78, 11.61, 8.17, 13.76, 
    1.5, 36.75, 137.77, -16.18, 4.18, -2.82, 0, 18.42, 53.19, 5.91,
    -16.18, 3.24, -2.82, 0, 53.19, 518.6, -16.18, 1.61, -2.82, 23, 0,
    70.92, 58.89, 13.08, 42.32, 57.67, -15.32, 1.76, -2.68, 18.42, 0,
    53.19, 6.33, -15.32, 2.01, -2.68, 0, 53.19, -15.32, 2.17, -2.68, 0,
    -1000, 76.83, 27.18, 0.02, 8.88, 13.08, 30, 48.72, 16.02, -15.32,
    1.69, -2.68, 0, 0.7, 53.19, 1128.85, 11.49, 53.19, 16.61, 209.84,
    1243.2, 23, 1.08


    I want to create a new list called balancelist using a recursive function (or any other way so I learn) such that I end up with a list with something like this:



    1539.91,1539.91-5.05,1539.91-5.05-(-2.82),...... 


    I have tried the following code but it doesn't work:



    balancefinal = 
    For[i = 1, i <= Length[x2], i++, x2[[i]] - x2[[i + 1]];
    Append[balancefinal]]









    share|improve this question

























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      Here is code:



      x2=1539.91, 5.05, -2.82, 0, 19, 135.93, 117.78, 11.61, 8.17, 13.76, 
      1.5, 36.75, 137.77, -16.18, 4.18, -2.82, 0, 18.42, 53.19, 5.91,
      -16.18, 3.24, -2.82, 0, 53.19, 518.6, -16.18, 1.61, -2.82, 23, 0,
      70.92, 58.89, 13.08, 42.32, 57.67, -15.32, 1.76, -2.68, 18.42, 0,
      53.19, 6.33, -15.32, 2.01, -2.68, 0, 53.19, -15.32, 2.17, -2.68, 0,
      -1000, 76.83, 27.18, 0.02, 8.88, 13.08, 30, 48.72, 16.02, -15.32,
      1.69, -2.68, 0, 0.7, 53.19, 1128.85, 11.49, 53.19, 16.61, 209.84,
      1243.2, 23, 1.08


      I want to create a new list called balancelist using a recursive function (or any other way so I learn) such that I end up with a list with something like this:



      1539.91,1539.91-5.05,1539.91-5.05-(-2.82),...... 


      I have tried the following code but it doesn't work:



      balancefinal = 
      For[i = 1, i <= Length[x2], i++, x2[[i]] - x2[[i + 1]];
      Append[balancefinal]]









      share|improve this question















      Here is code:



      x2=1539.91, 5.05, -2.82, 0, 19, 135.93, 117.78, 11.61, 8.17, 13.76, 
      1.5, 36.75, 137.77, -16.18, 4.18, -2.82, 0, 18.42, 53.19, 5.91,
      -16.18, 3.24, -2.82, 0, 53.19, 518.6, -16.18, 1.61, -2.82, 23, 0,
      70.92, 58.89, 13.08, 42.32, 57.67, -15.32, 1.76, -2.68, 18.42, 0,
      53.19, 6.33, -15.32, 2.01, -2.68, 0, 53.19, -15.32, 2.17, -2.68, 0,
      -1000, 76.83, 27.18, 0.02, 8.88, 13.08, 30, 48.72, 16.02, -15.32,
      1.69, -2.68, 0, 0.7, 53.19, 1128.85, 11.49, 53.19, 16.61, 209.84,
      1243.2, 23, 1.08


      I want to create a new list called balancelist using a recursive function (or any other way so I learn) such that I end up with a list with something like this:



      1539.91,1539.91-5.05,1539.91-5.05-(-2.82),...... 


      I have tried the following code but it doesn't work:



      balancefinal = 
      For[i = 1, i <= Length[x2], i++, x2[[i]] - x2[[i + 1]];
      Append[balancefinal]]






      webmathematica mathematica-online






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 13 mins ago









      kglr

      169k8192395




      169k8192395










      asked 1 hour ago









      beemen

      462




      462




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          7
          down vote













          If you consider Accumulate a recursive function, then you could do:



          2 x2[[1]] - Accumulate[x2]


          This is much faster than using something like FoldList. For example:



          x2 = RandomReal[-10, 10, 10^6];

          r1 = 2 x2[[1]] - Accumulate[x2]; //AbsoluteTiming
          r2 = FoldList[Subtract, x2]; //AbsoluteTiming

          MinMax[r1 - r2]



          0.005461, Null



          0.161103, Null



          -3.86535*10^-12, 1.65983*10^-11







          share|improve this answer



























            up vote
            4
            down vote













            You can Fold Subtract on x2:



            FoldList[Subtract, x2]



            1539.91,1534.86,1537.68,1537.68,1518.68,1382.75,1264.97,1253.36,1245.19,1231.43,1229.93,1193.18,1055.41,1071.59,1067.41,1070.23,1070.23,1051.81,998.62,992.71,1008.89,1005.65,1008.47,1008.47,955.28,436.68,452.86,451.25,454.07,431.07,431.07,360.15,301.26,288.18,245.86,188.19,203.51,201.75,204.43,186.01,186.01,132.82,126.49,141.81,139.8,142.48,142.48,89.29,104.61,102.44,105.12,105.12,1105.12,1028.29,1001.11,1001.09,992.21,979.13,949.13,900.41,884.39,899.71,898.02,900.7,900.7,900.,846.81,-282.04,-293.53,-346.72,-363.33,-573.17,-1816.37,-1839.37,-1840.45




            If you have to use For here is a modification of your code that gives the correct result:



            balancefinal = x2[[1]];
            For[i = 1, i < Length[x2], i++, AppendTo[balancefinal, balancefinal[[-1]] - x2[[i + 1]]]]

            balancefinal == FoldList[Subtract, x2]



            True







            share|improve this answer






















              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: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              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%2f185044%2fapplying-a-recursive-function-to-a-list-to-get-another-list%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
              7
              down vote













              If you consider Accumulate a recursive function, then you could do:



              2 x2[[1]] - Accumulate[x2]


              This is much faster than using something like FoldList. For example:



              x2 = RandomReal[-10, 10, 10^6];

              r1 = 2 x2[[1]] - Accumulate[x2]; //AbsoluteTiming
              r2 = FoldList[Subtract, x2]; //AbsoluteTiming

              MinMax[r1 - r2]



              0.005461, Null



              0.161103, Null



              -3.86535*10^-12, 1.65983*10^-11







              share|improve this answer
























                up vote
                7
                down vote













                If you consider Accumulate a recursive function, then you could do:



                2 x2[[1]] - Accumulate[x2]


                This is much faster than using something like FoldList. For example:



                x2 = RandomReal[-10, 10, 10^6];

                r1 = 2 x2[[1]] - Accumulate[x2]; //AbsoluteTiming
                r2 = FoldList[Subtract, x2]; //AbsoluteTiming

                MinMax[r1 - r2]



                0.005461, Null



                0.161103, Null



                -3.86535*10^-12, 1.65983*10^-11







                share|improve this answer






















                  up vote
                  7
                  down vote










                  up vote
                  7
                  down vote









                  If you consider Accumulate a recursive function, then you could do:



                  2 x2[[1]] - Accumulate[x2]


                  This is much faster than using something like FoldList. For example:



                  x2 = RandomReal[-10, 10, 10^6];

                  r1 = 2 x2[[1]] - Accumulate[x2]; //AbsoluteTiming
                  r2 = FoldList[Subtract, x2]; //AbsoluteTiming

                  MinMax[r1 - r2]



                  0.005461, Null



                  0.161103, Null



                  -3.86535*10^-12, 1.65983*10^-11







                  share|improve this answer












                  If you consider Accumulate a recursive function, then you could do:



                  2 x2[[1]] - Accumulate[x2]


                  This is much faster than using something like FoldList. For example:



                  x2 = RandomReal[-10, 10, 10^6];

                  r1 = 2 x2[[1]] - Accumulate[x2]; //AbsoluteTiming
                  r2 = FoldList[Subtract, x2]; //AbsoluteTiming

                  MinMax[r1 - r2]



                  0.005461, Null



                  0.161103, Null



                  -3.86535*10^-12, 1.65983*10^-11








                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 1 hour ago









                  Carl Woll

                  63.4k282163




                  63.4k282163




















                      up vote
                      4
                      down vote













                      You can Fold Subtract on x2:



                      FoldList[Subtract, x2]



                      1539.91,1534.86,1537.68,1537.68,1518.68,1382.75,1264.97,1253.36,1245.19,1231.43,1229.93,1193.18,1055.41,1071.59,1067.41,1070.23,1070.23,1051.81,998.62,992.71,1008.89,1005.65,1008.47,1008.47,955.28,436.68,452.86,451.25,454.07,431.07,431.07,360.15,301.26,288.18,245.86,188.19,203.51,201.75,204.43,186.01,186.01,132.82,126.49,141.81,139.8,142.48,142.48,89.29,104.61,102.44,105.12,105.12,1105.12,1028.29,1001.11,1001.09,992.21,979.13,949.13,900.41,884.39,899.71,898.02,900.7,900.7,900.,846.81,-282.04,-293.53,-346.72,-363.33,-573.17,-1816.37,-1839.37,-1840.45




                      If you have to use For here is a modification of your code that gives the correct result:



                      balancefinal = x2[[1]];
                      For[i = 1, i < Length[x2], i++, AppendTo[balancefinal, balancefinal[[-1]] - x2[[i + 1]]]]

                      balancefinal == FoldList[Subtract, x2]



                      True







                      share|improve this answer


























                        up vote
                        4
                        down vote













                        You can Fold Subtract on x2:



                        FoldList[Subtract, x2]



                        1539.91,1534.86,1537.68,1537.68,1518.68,1382.75,1264.97,1253.36,1245.19,1231.43,1229.93,1193.18,1055.41,1071.59,1067.41,1070.23,1070.23,1051.81,998.62,992.71,1008.89,1005.65,1008.47,1008.47,955.28,436.68,452.86,451.25,454.07,431.07,431.07,360.15,301.26,288.18,245.86,188.19,203.51,201.75,204.43,186.01,186.01,132.82,126.49,141.81,139.8,142.48,142.48,89.29,104.61,102.44,105.12,105.12,1105.12,1028.29,1001.11,1001.09,992.21,979.13,949.13,900.41,884.39,899.71,898.02,900.7,900.7,900.,846.81,-282.04,-293.53,-346.72,-363.33,-573.17,-1816.37,-1839.37,-1840.45




                        If you have to use For here is a modification of your code that gives the correct result:



                        balancefinal = x2[[1]];
                        For[i = 1, i < Length[x2], i++, AppendTo[balancefinal, balancefinal[[-1]] - x2[[i + 1]]]]

                        balancefinal == FoldList[Subtract, x2]



                        True







                        share|improve this answer
























                          up vote
                          4
                          down vote










                          up vote
                          4
                          down vote









                          You can Fold Subtract on x2:



                          FoldList[Subtract, x2]



                          1539.91,1534.86,1537.68,1537.68,1518.68,1382.75,1264.97,1253.36,1245.19,1231.43,1229.93,1193.18,1055.41,1071.59,1067.41,1070.23,1070.23,1051.81,998.62,992.71,1008.89,1005.65,1008.47,1008.47,955.28,436.68,452.86,451.25,454.07,431.07,431.07,360.15,301.26,288.18,245.86,188.19,203.51,201.75,204.43,186.01,186.01,132.82,126.49,141.81,139.8,142.48,142.48,89.29,104.61,102.44,105.12,105.12,1105.12,1028.29,1001.11,1001.09,992.21,979.13,949.13,900.41,884.39,899.71,898.02,900.7,900.7,900.,846.81,-282.04,-293.53,-346.72,-363.33,-573.17,-1816.37,-1839.37,-1840.45




                          If you have to use For here is a modification of your code that gives the correct result:



                          balancefinal = x2[[1]];
                          For[i = 1, i < Length[x2], i++, AppendTo[balancefinal, balancefinal[[-1]] - x2[[i + 1]]]]

                          balancefinal == FoldList[Subtract, x2]



                          True







                          share|improve this answer














                          You can Fold Subtract on x2:



                          FoldList[Subtract, x2]



                          1539.91,1534.86,1537.68,1537.68,1518.68,1382.75,1264.97,1253.36,1245.19,1231.43,1229.93,1193.18,1055.41,1071.59,1067.41,1070.23,1070.23,1051.81,998.62,992.71,1008.89,1005.65,1008.47,1008.47,955.28,436.68,452.86,451.25,454.07,431.07,431.07,360.15,301.26,288.18,245.86,188.19,203.51,201.75,204.43,186.01,186.01,132.82,126.49,141.81,139.8,142.48,142.48,89.29,104.61,102.44,105.12,105.12,1105.12,1028.29,1001.11,1001.09,992.21,979.13,949.13,900.41,884.39,899.71,898.02,900.7,900.7,900.,846.81,-282.04,-293.53,-346.72,-363.33,-573.17,-1816.37,-1839.37,-1840.45




                          If you have to use For here is a modification of your code that gives the correct result:



                          balancefinal = x2[[1]];
                          For[i = 1, i < Length[x2], i++, AppendTo[balancefinal, balancefinal[[-1]] - x2[[i + 1]]]]

                          balancefinal == FoldList[Subtract, x2]



                          True








                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited 6 mins ago

























                          answered 1 hour ago









                          kglr

                          169k8192395




                          169k8192395



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f185044%2fapplying-a-recursive-function-to-a-list-to-get-another-list%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Comments

                              Popular posts from this blog

                              Long meetings (6-7 hours a day): Being “babysat” by supervisor

                              Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                              Confectionery