From: miguelwon on
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

From: Bill Rowe on
On 7/24/10 at 5:07 AM, miguelwon(a)gmail.com (miguelwon) wrote:

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

Assuming the variables you want to assign values to do not have
values already assigned to them, then the following will do what
you want.

In[1]:= Set[Evaluate[ToExpression["c" <> ToString[#]]], #] & /@
Range[5];
{c1, c5}

Out[2]= {1,5}

This doesn't work if the variables have values assigned to them
since the Evaluate will cause the variable to be evaluated to
its value resulting in a non-sensical attempt to assign a number
to a number.


From: magma on
On Jul 24, 11:07 am, miguelwon <miguel...(a)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?

From: James Stein on
Perhaps something like this?

n = 4; (* # of desired variables *)
co = Table["co" <> ToString[i], {i, 1, n}];
For[ i = 1, i <= n, i++, co [ [ i ] ] = i ^ 2 ] ;



On Sat, Jul 24, 2010 at 2:07 AM, miguelwon <miguelwon(a)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
>
>
From: James Stein on
Big Correction to my preceding post!
Tricked by my own cockpit error.
Apologies to all. Try this instead:

n = 4;(*# of desired variables*)
Quiet [ Remove["co*"], Remove::"rmnsm"];
set = Function[#1 = #2];
Table[Symbol["co" <> ToString[ i ] ], {i, 1, n}]
For[i = 1, i <= n, i++, set[%[ [ i ] ], i^3] ]
{co1, co2, co3}

On Sat, Jul 24, 2010 at 4:39 PM, James Stein <mathgroup(a)stein.org> wrote:

> Perhaps something like this?
>
> n = 4; (* # of desired variables *)
> co = Table["co" <> ToString[i], {i, 1, n}];
> For[ i = 1, i <= n, i++, co [ [ i ] ] = i ^ 2 ] ;
>
>
>
>
> On Sat, Jul 24, 2010 at 2:07 AM, miguelwon <miguelwon(a)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
>>
>>
>


 |  Next  |  Last
Pages: 1 2
Prev: trading
Next: Experimental Data Analyst