Mathematica 9 is now available
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: [mg111259] Re: Assign value to variable
  • From: Peter Breitfeld <phbrf at t-online.de>
  • Date: Sun, 25 Jul 2010 02:00:02 -0400 (EDT)
  • References: <i2eagg$q5b$1@smc.vnet.net>

miguelwon 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
>

The error occurs, because you tried to assign the value 0 to an
Expression with Head Symbol. One way to make lists of symbols is to use
Array like this:

Array[co,5]  gives
{co[1], co[2], co[3], co[4], co[5]}

If you like to name your variables co1, co2,... (bad idea) you could do the
following

CC=Array["cc"<>ToString[#]&,5]
{"cc1", "cc2", "cc3", "cc4", "cc5"}

These are not Symbols but Strings, which is bad.

So I would not recommend this, because you can't easily assign values to
the list, whereas in the first case it's easy:

Do[co[i]=10+i,{i,10}]

co/@Range[5]
{11, 12, 13, 14, 15}

-- 
_________________________________________________________________
Peter Breitfeld, Bad Saulgau, Germany -- http://www.pBreitfeld.de


  • Prev by Date: tweaking VectorPlot...
  • Next by Date: Re: Subscript in AxesLabel
  • Previous by thread: Re: Assign value to variable
  • Next by thread: Re: trading