Re: Assign value to variable
- To: mathgroup at smc.vnet.net
- Subject: [mg111244] Re: Assign value to variable
- From: magma <maderri2 at gmail.com>
- Date: Sun, 25 Jul 2010 01:57:11 -0400 (EDT)
- References: <i2eagg$q5b$1@smc.vnet.net>
On Jul 24, 11:07 am, miguelwon <miguel... 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 I would rather use: Table[co[i] = 0, {i, 100}] and would advise you NOT to use co1, co2, etc for indexed variables. Things like co1 and co5 are difficult to manage (for ex. make a list), while things like co[1] and co[5] are very flexible, as you see from my simple Table above. Also: one must be careful what one puts on the LHS of "=". Not everything is accepted. In this case you were trying to give a value to Symbol, which is not possible. Even using Upvalues does not help. Last , but not least,why use For, when you can use Table?