Bookmark and Share

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

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

Re: st: A question for those using Mathematica

From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: A question for those using Mathematica
Date   Fri, 7 Jan 2011 08:51:28 +0000

I suggest that this discussion was inappropriate for Statalist.

Mentions of software X are appropriate when questions are of the form

Does anyone know how to interface Stata and X?
Does anyone know the Stata equivalent of X?

and in that way are focused on Stata use. The examples are not
exclusive or exhaustive.

Here it is the other way round. In fact the Stata example is
incidental as the question is very nearly How do I loop in

On this list we have nothing against X, whatever it is. But typically
X questions are better directed elsewhere. And for most tastes the
traffic on this list is not too light. Once X questions are allowed,
the traffic might explode, and I think that would not be good.


On Fri, Jan 7, 2011 at 3:15 AM, Quang Nguyen <[email protected]> wrote:

> 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 <[email protected]> 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

*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index