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'm new to Mathematica. Below is the code I have trouble with.

    Subscript[C, k][x_, y_]: = 
          Subscript[q, 2 k - 1] + x*Subscript[q, 2 k] - (Subscript[q, l] + y*Subscript[q, l + 1])
 {k, 3, 5}, {l, 3}]

In this loop, I want k=3 map to l=1, k=4 map to l=2, and k=5 map to l=3. However in this loop function, it loops through l=1,2,3 when k=3 and so on. So is there an automatic way or code to stop the certain looping?

share|improve this question

closed as off-topic by bobthechemist, m_goldberg, István Zachar, Silvia, rm -rf Jan 16 at 17:02

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." – bobthechemist, m_goldberg, István Zachar, Silvia, rm -rf
If this question can be reworded to fit the rules in the help center, please edit the question.

Quick fix would be to leave only one iterator, k and instead of l put k-2. –  Kuba Jan 14 at 1:00
And Printing a function definition will give you Null. Not really useful. –  asterix314 Jan 14 at 3:22
Cheers Guys. The reason I have k and l is that they will make my following programming much easier. Just curious is there other ways or code can handle that? Thanks again. –  user11726 Jan 14 at 10:05

1 Answer 1

up vote 0 down vote accepted

If you are aiming to use loop to define functions you could use MapThread:

MapThread[(Subscript[C, #1][x, y] := 
     Subscript[q, 2 #1 - 1] + 
      x*Subscript[q, 2 #1] - (Subscript[q, #2] + 
        y*Subscript[q, #2 + 1])) &, {Range[3, 5], Range[3]}];'

Now you can test functions are defined:

Table[Subscript[C, j][a, b], {j, Range[3, 5]}]

This yields:

{-Subscript[q, 1] - b Subscript[q, 2] + Subscript[q, 5] + 
  a Subscript[q, 6], -Subscript[q, 2] - b Subscript[q, 3] + Subscript[
  q, 7] + a Subscript[q, 8], -Subscript[q, 3] - b Subscript[q, 4] + 
  Subscript[q, 9] + a Subscript[q, 10]}
share|improve this answer
Cheers ubpdqn! That's exactly what I wanted. Much appreciated! –  user11726 Jan 14 at 12:47

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