MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: defining consecutive variables

  • To: mathgroup at
  • Subject: [mg99559] Re: defining consecutive variables
  • From: Sebastien Roy <roys at>
  • Date: Fri, 8 May 2009 00:13:40 -0400 (EDT)
  • References: <gtp199$joi$>

On May 5, 5:35 am, Jason <jbig... at> 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?

The solution with the form q[n] is fine, but if you really need, for
typesetting reason, to have the variable labeled as in Subscript[q,n]
instead, then this will work:

f[x_]:= Subscript[q,x] = RandomReal[{0, x}, {5, 5}]

You can use the palette (or use Ctrl + _ ) instead of typing Subscriptp

  • Prev by Date: Re: math glyphs from non-Mathematica font
  • Next by Date: Re: Help with an error with NonlinearModelFit, Mathematica 7.0
  • Previous by thread: Re: defining consecutive variables
  • Next by thread: Reading csv with ;