MathGroup Archive 2009

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

Search the Archive

Re: Assigning Values to Many Symbols at Once

  • To: mathgroup at smc.vnet.net
  • Subject: [mg101730] Re: Assigning Values to Many Symbols at Once
  • From: pfalloon <pfalloon at gmail.com>
  • Date: Wed, 15 Jul 2009 07:11:43 -0400 (EDT)
  • References: <h3hq0s$5b5$1@smc.vnet.net>

On Jul 14, 9:26 pm, Gregory Lypny <gregory.ly... at videotron.ca> wrote:
> Hello everyone,
>
> I can create a bunch of symbols on the fly using the Symbol command,  
> as in
>
>         Table[Symbol["x" <> ToString[i]], {i, 5}]
>
> to get
>
>         {x1,x2,x3,x4,x5}
>
> But how can I assign values to these all at once?
>
> Regards,
>
>         Gregory

The following may be what you're after:

    Table[symName = "x"<>ToString[i]; Evaluate[Symbol[symName]] = i^2,
{i, 10}]

Note that using Evaluate inside the Set (=) command causes the LHS to
be evaluated to the symbol, to which the RHS can then be assigned.

To remove these definitions you can use

    Clear["x*"];

Another way to do the same thing is:

    Evaluate[Table[Symbol["x"<>ToString[i]], {i, 10}]] = Range[10]^2

However, at the end of the day you may not really gain much using this
approach over just using a single variable with indices, e.g.

    Table[x[i] = i^2, {i,10}]

Cheers,
Peter.


  • Prev by Date: Maximize question
  • Next by Date: Re: Plot Label at bottom with quote marks
  • Previous by thread: Re: Assigning Values to Many Symbols at Once
  • Next by thread: Re: Assigning Values to Many Symbols at Once