6
$\begingroup$

Here is a list:

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]]
$\endgroup$

2 Answers 2

11
$\begingroup$

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}

$\endgroup$
8
  • $\begingroup$ What version of Mathematica are you using? On my machine I have r2 more efficient. $\endgroup$ Commented Nov 1, 2018 at 2:55
  • $\begingroup$ @ΑλέξανδροςΖεγγ r1 is much faster than r2 on all of my versions, from M9 to M11 on MacOS. $\endgroup$
    – Carl Woll
    Commented Nov 1, 2018 at 3:11
  • $\begingroup$ So it is very strange then. In[35]:= RepeatedTiming[FoldList[Subtract, x2];] RepeatedTiming[2 First[x2] - Accumulate[x2];] Out[35]= {0.000015, Null} Out[36]= {0.000037, Null} $\endgroup$ Commented Nov 1, 2018 at 3:28
  • $\begingroup$ BTW, v11.3 on Win10 $\endgroup$ Commented Nov 1, 2018 at 3:30
  • $\begingroup$ @ΑλέξανδροςΖεγγ I think you're using the OP x2, and not the x2 in my answer. $\endgroup$
    – Carl Woll
    Commented Nov 1, 2018 at 3:30
8
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.