Re: Assigning Values to Many Symbols at Once
- To: mathgroup at smc.vnet.net
- Subject: [mg101789] Re: Assigning Values to Many Symbols at Once
- From: pfalloon <pfalloon at gmail.com>
- Date: Fri, 17 Jul 2009 05:03:19 -0400 (EDT)
- References: <200907141200.IAA05959@smc.vnet.net> <h3n5v7$2hj$1@smc.vnet.net>
On Jul 16, 10:20 pm, Gregory Lypny <gregory.ly... at videotron.ca> wrote: > Thank you, Ssezi, Zach, and Leonid, for your thoughtful responses. > > Ssezi's solution works both in assigning string and numerical values > subject to the qualifier, noted by everyone, that you can only do it > once. > > MapThread[(#=#2)&,{Table[Symbol["x" <> ToString[i]], {i= , > 5}],Table["value" <> ToString[i],{i,5}]}] > > Leonid and Zach, both of you suggested direct list-to-list > assignments, which is what I thought should work but does not with you = > create the list of new symbols using Table or mapping. It does work = > if you create the symbol list manually, For example, typing > > {x1, x2} = {9, 11} > > works as expected, creating two new symbols, x1 with a value of 9 and > x2 with a value of 10. However, > > Table[Symbol["x" <> ToString[i]], {i, 5}] = {1,2,3,4,5} > > and > > Table[Symbol["x"<>ToString[i]],{i,5}]=Table[i,{i,5}] > > result in the error message > > Set::write: Tag Table in Table[Symbol[xabc<>ToString[i]],= {i,5}] is > Protected. > > I still haven't worked through Leonid's workaround. > > Thanks again, > > Gregory > > > > Ssezi wrote: > > One option is something like: > > > MapThread[(#=#2)&,{Table[Symbol["x" <> ToString[i]], {i, > > 5}],Table["value" <> ToString[i],{i,5}]}] > > > but you can't do this twice since the once the symbols are > > associated with values the evaluator with evaluate the symbol then > > attempt to assign the value resulting in assignment to a raw object. > > > Regards, > > > Ssezi > Zach wrote: > > Hi Gregory, > > > You can simply do list-to-list assignments: > > {x1,x2,x3,x4,x5}={value1,value2,value3,value4,value5} > > > in whatever way you want, e.g. > > Table[Symbol["x"<>ToString[i]],{i,5}]=Table[i,{i,5}] > > > Note that this is especially useful for doing parallel calculations > > to obtain values not derived from loops. E.g. > > {x1,x2}={ParallelEvaluate[(*some long calculation*),{Kernels[] > > [[1]]}],ParallelEvaluate[(*some different long calculation*), > > {Kernels[][[2]]}]} > > > Cheers, > > Zach > Leonid Shifrin wrote: > > Hi Gregory, > > > If your symbols don't have values, you can simply use Set: > > > Table[Symbol["x" <> ToString[i]], {i, 5}] = {1,2,3,4,5} > > > for instance. If they do have values, this is a bit more work. The > > following > > will do what you presumably want, in both cases: > > > In[1] = > > > ClearAll[assignToSymbols]; > > SetAttributes[assignToSymbols, HoldFirst]; > > assignToSymbols[base_String, {startIndex_Integer, endIndex_Integer}, > > values_List] /; endIndex - startIndex + 1 == Length[values]= := > > assignToSymbols @@ > > Append[Unevaluated /@ Thread[ > > Table[ > > ToExpression[base <> ToString[i], InputForm, Hold], > > {i, startIndex, endIndex}], Hold], values]; > > > assignToSymbols[symbs : {___Symbol}, values_List] /; > > Length[Hold @@ symbs] == Length[values] := > > Unevaluated[symbs] = values; > > > In[2]= > > Clear[x1, x2, x3, x4, x5]; > > assignToSymbols["x", {1, 5}, Range[5]]; > > {x1, x2, x3, x4, x5} > > > Out[2] = {1, 2, 3, 4, 5} > > > In[3] = assignToSymbols["x", {1, 5}, Range[5, 9]] > > > Out[3] = {5, 6, 7, 8, 9} > > > Regards, > > Leonid > > > On Tue, Jul 14, 2009 at 5:00 AM, Gregory Lypny <gregory.ly...@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 Gregory, (As I and several others suggested in earlier responses) you can make this work by wrapping the LHS in an Evaluate: In[81]:= Evaluate[Table[Symbol["x"<>ToString[i]],{i,5}]]=Table[i,{i, 5}] Out[81]= {1, 2, 3, 4, 5} In[83]:= {x1,x2,x3,x4,x5} Out[83]= {1, 2, 3, 4, 5} Cheers, Peter.
- References:
- Assigning Values to Many Symbols at Once
- From: Gregory Lypny <gregory.lypny@videotron.ca>
- Assigning Values to Many Symbols at Once