Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

From |
Quang Nguyen <quangn@gmail.com> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: A question for those using Mathematica |

Date |
Thu, 6 Jan 2011 17:15:41 -1000 |

Many thanks Greg for your very thoughtful answer. I would like to enclose Greg's reply here for your references. Here is my question: Do you know an equavalent way of using the loop index in a variable name in Mathematica. In particular, in Stata can we define variable such as: var`i' ; where i is an index of a loop (e.g., "for" loop Greg's reply is: I don’t know if it’s still relevant or if you solved your problem, but here’s the answer to your question. I do not have the rights post on the Stata Listserv, so I’d really appreciate it if you’d respond to your own post and add my answer so others may learn. In Mathematica, all code representation is symbolic. All notation is either a string, a symbol, or a special character. Symbols and special characters combine into expressions, and everything then further combines to form “boxes”. In this way you can represent EVERYTHING in any formatting/style/color, etc etc. Aside from these core definitions, such as symbol, string, integer, real, etc, everything else is an expression combining these things. For example input x+5 will be re-sorted internally to 5+x and will be stored as Plus[5,x], where 5 will be an exact integer (important when you want rational results), x will be a symbol, and “Plus” will also be a symbol which will actually be a “Head” of the “Sequence” 5,x. So First[x+5] will return 5, (x+5)[[-1]] will return x, and Head[x+5] will return Plus. To go from expression to a string you use ToString, and ToExpression to travel in the other direction. Several functions exist to travel into the world of numbers, but you have to be careful with precision and other aspects of numerical representation in Mathematica. For your specific case, you should just generate a symbol (more closely an entire expression consisting of just one symbol) from a string which contains your iterator variable’s value: For[i=0,i<10,i++,Print[Row[{“x”<>i,” = “,ToExpression[“x”<>i]}]]; If you need to assign to such a variable, the Head for = is Set, but it has a HoldFirst attribute, which means that you will try to assign a value to the ToExpression[…] expression, not to the variable, which will be protected from assignment. The solution is to use the Evaluate head, which causes evaluation of the held parameter: For[i=0,i<10,i++, Set[Evaluate@ToExpression[“x”<>i] , i] ]; or simply For[i=0,i<10,i++, Evaluate@ToExpression[“x”<>i] = i]; Of course this goes well beyond simple iterators. You can create variables out of column names returned from SQLColumns or entire ad-hoc expressions combining very complex generation rules. Instead of using player0 and player1 to represent human and computer player, you can actually use the word in your variable name and then create a variable based on playr type, game/move number, so on and so forth. Having said all that, I question practicality and readability of this approach, and auto-optimization of ad-hoc expressions. In your specific example, I think you’d be much better off just using arrays: x[i], or indexes: xi You can also create x[n_] where x is a function of n, but instead of assigning it a latent value, computed at the time of evaluation (:=), just assign the value you need. Similarly, you can use a SparseArray, which allows you to have x[5] and x[10] with nothing between (not just 0), but I prefer regular Lists with Null elements x={1,,,,5}; Print@x[5] prints 5. Good luck! Hope the description helps better understand and use Mathematica. -Greg Klopper On Tue, Sep 28, 2010 at 2:37 AM, Quang Nguyen <quangn@gmail.com> wrote: > > Do you know an equavalent way of using the loop index in a variable > name in Mathematica. In particular, in Stata can we define variable > such as: var`i' ; where i is an index of a loop (e.g., "for" loop > > Many thanks! > > -- > "My father gave me the greatest gift anyone could give another person, > he believed in me." - Jim Valvano > * > * For searches and help try: > * http://www.stata.com/help.cgi?search > * http://www.stata.com/support/statalist/faq > * http://www.ats.ucla.edu/stat/stata/ -- "My father gave me the greatest gift anyone could give another person, he believed in me." - Jim Valvano * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/

**Follow-Ups**:**Re: st: A question for those using Mathematica***From:*Nick Cox <njcoxstata@gmail.com>

- Prev by Date:
**Re: st: Update of cm2in** - Next by Date:
**st: tempvar in debugging** - Previous by thread:
**st: Testing for binary mediation using logit and survey design adjustments (weights, etc.)** - Next by thread:
**Re: st: A question for those using Mathematica** - Index(es):