Re: defining consecutive variables

*To*: mathgroup at smc.vnet.net*Subject*: [mg99442] Re: defining consecutive variables*From*: Szabolcs <szhorvat at gmail.com>*Date*: Wed, 6 May 2009 05:20:24 -0400 (EDT)*References*: <gtp199$joi$1@smc.vnet.net>

On May 5, 12:35 pm, Jason <jbig... at uoregon.edu> wrote: > I have a code where I need to define a large number of variables as matri= ces, called q1,q2,q3......qn. I'd like to be able to define them all withou= t writing out n assignment lines, so a Do loop seems appropriate to me but = I don't know how to assign sequential variable names. This gets the job don= e but it is really ugly IMO > > f[x_] := Table[x RandomReal[], {n, 5}, {np, 5}](*for example*) > > Do[ToExpression["q" <> ToString[n] <> "=f[n]"], {n, 0, 40}] > > at the end of which I have 41 matrices which I can call as q0,q1, etc. Is= this the best way to accomplish this task? > Consider using expressions of the form q[1], q[2], etc. Are you doing anything with these variables that requires them to be atomic symbols (instead of compound expressions like q[1])? Do[q[i] = f[i], {i,0,40}] Also, do you really need all these matrices to have a unique name? It sounds like you might need to iterate over them several times (or perform identical calculations with all of them). Couldn't you just use a simple list of matrices instead?