Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

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

marked as duplicate by Mr.Wizard Jan 18 '15 at 15:28

This question was marked as an exact duplicate of an existing question.

Quick fix would be to leave only one iterator, k and instead of l put k-2. – Kuba Jan 14 '14 at 1:00
And Printing a function definition will give you Null. Not really useful. – asterix314 Jan 14 '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 '14 at 10:05
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 '14 at 12:47

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