Re: defining consecutive variables

*To*: mathgroup at smc.vnet.net*Subject*: [mg99494] Re: defining consecutive variables*From*: Tom Burton <tburton at brahea.com>*Date*: Wed, 6 May 2009 05:44:31 -0400 (EDT)

You seem to want to cache f[x] for a set of x. With your example, slightly rewritten, most people would do that as follows: f[x_] := f[x] = RandomReal[{0, x}, {5, 5}] so that f is both a function and an indexed variable: f[1] evaluates with x==1 and stores the result in f[1], etc. Then evaluate f at all desired values of x. Your example is Scan[f, Range[0, 40]] but in place of Range[0,40] you can put any list of x's. Then indexed variables f[0] through f[40] are defined. Cache instead into indexed variables q[x] with f[x_] := q[x] = RandomReal[{0, x}, {5, 5}] For most purposes, q[x] is more convenient than qx {x = 0, 1, 2, ..., or whatever}, but if you insist, you can write f[x_] := Evaluate[ToExpression["q" <> ToString[x]]] = RandomReal[{0, x}, {5, 5}] (Evaluate is needed because Set (=) Holds the left-hand member otherwise.) Or you could do it the way you suggested: >f[x_] := RandomReal[{0, x}, {5, 5}] >Do[ToExpression["q" <> ToString[n] <> "=f[n]"], {n, 0, 40}] Tom