Skip to main content
added 1 character in body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929

You can Fold Subtract on x2:

FoldList[Subtract,FoldList[Subtract] x2]@ 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

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

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

added 273 characters in body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929

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

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}

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

added 130 characters in body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929

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}

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}

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}

Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929
Loading