Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a list F = {382.856, 638.53, 915.482, 1192.44, 1469.39, 1746.34, 2023.29, 2181.91} And I want to use it to calculate V:

enter image description here

I think it should be something like: V = Table[ V[i] = V[i+1] + F[i], {...loop in reverse order}], I'm not sure about the expression, and didn't find options to excute loop in reverse order.

In Mathematica's reference, I only find that Do[expr,{i,imax}] , does this mean that the loop can only go in increasing order?

share|improve this question

closed as off-topic by Michael E2, Pickett, m_goldberg, Leonid Shifrin, Simon Woods Apr 21 at 14:20

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Michael E2, Pickett, m_goldberg, Leonid Shifrin, Simon Woods
If this question can be reworded to fit the rules in the help center, please edit the question.

1  
You're not looking carefully... The same works for Table. –  Michael E2 Apr 21 at 12:52

1 Answer 1

up vote 2 down vote accepted

Table also supports iterating in reverse order (from the docs):

Table[expr,{i,Subscript[i, min],Subscript[i, max],di}]
  uses steps di.

Table[i, {i, 10, 1, -1}]
out: {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}

What you want to do looks more like a recursive definition. For example:

f = {382.856, 638.53, 915.482, 1192.44, 1469.39, 1746.34, 2181.91}
v[8] = f[[8]];
v[i_] := v[i + 1] + f[[i]]

As a Table:

Table[v[i], {i, 8, 1, -1}]
out: {382.856, 638.53, 915.482, 1192.44, 1469.39, 1746.34, 2023.29, 2181.91}

More elegantly, your example can be expressed as FoldList:

FoldList[#1 + #2 &, 0, f]

Or in this case even:

Accumulate[f]
share|improve this answer
    
What is the difference between f[[8]] and f[8]? –  cqcn1991 Apr 21 at 13:12
    
f[[i]] is specifically index lookup in a list/array. f[i] is applying f as a function to parameter i. –  Justin Kaeser Apr 21 at 13:31

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