Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

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

Search the Archive

Re: Assign value to variable

  • To: mathgroup at smc.vnet.net
  • Subject: [mg111263] Re: Assign value to variable
  • From: Leonid Shifrin <lshifr at gmail.com>
  • Date: Sun, 25 Jul 2010 02:00:47 -0400 (EDT)

This will work, regardless of whether the variables already had some values
or not:

Table[ToExpression["co" <> ToString[i], InputForm,
  Function[s, s = 0, HoldAll]], {i, 100}]

I would recommend using indexed variables like co[1], co[2] etc
instead of co1, co2, etc - then it is even much easier:

In[10]:= Clear[co];
In[11]:= Do[co[i] = 0, {i, 100}]

In[12]:= co /@ Range[10]

Out[12]= {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

Indexed variables are generally easier to manipulate than Symbols,
particularly when
you have many of them and want to manipulate them programmatically.

Regards,
Leonid


On Sat, Jul 24, 2010 at 1:07 PM, miguelwon <miguelwon at gmail.com> wrote:

> Hello.
>
> I'm working with some expressions that are dependent of several
> variables co1, co2, ... , con. I would like to know how can I assign
> values iteratively to some of these variables. I tried:
>
> For[i=1,i<=100,i++,
> Symbol["co"<>ToString[i]]=0;
> ];
>
> but it doesn't work. For each iteration it says coi is Protected.
> Can someone help me?
>
> Thanks
>
>



  • Prev by Date: Re: Assign value to variable
  • Next by Date: Linking my fortran subroutines with Mathematica
  • Previous by thread: Re: Assign value to variable
  • Next by thread: Re: Assign value to variable