Skip to main content
4 of 4
added 27 characters in body
Albert Retey
  • 23.6k
  • 60
  • 104

How to use local variables with indices in a Mathematica Block environment?

I have the following code:

Subscript[f, i_][x_] := Block[{a},
  Subscript[a, i] = 3 x;
  Subscript[a, 1]
]

If I evaluate for example Subscript[f,1][2], it evaluates to 6. After that asking what Subscript[a,1] is also gives me six. However, I want a to be a local variable.

My question: how can I define a local variable while using indices...?

Note: the original post mentioned that evaluating Subscript[f,1][2] = 6 would lead to the described behaviour, which just isn't true. A reasonable guess is that this was ment to mean that Subscript[f,1][2] was to be evaluated and it would evaluate to 6 (which it does). After that Subscript[a,1] will evaluate to 6 as reported.

Bee