Applying a recursive function to a list to get another list
Clash 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]]
webmathematica mathematica-online
add a comment |Â
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]]
webmathematica mathematica-online
add a comment |Â
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]]
webmathematica mathematica-online
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
webmathematica mathematica-online
edited 12 mins ago
kglr
169k8192395
169k8192395
asked 1 hour ago
beemen
462
462
add a comment |Â
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
answered 1 hour ago
Carl Woll
63.4k282163
63.4k282163
add a comment |Â
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
edited 5 mins ago
answered 1 hour ago
kglr
169k8192395
169k8192395
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password