# I want to set $x = \{x_1, x_2, \cdots, x_n\}$

I want to get $$\{x_1, x_2, \cdots, x_n\}$$.
I want to use the $$x_i$$ as a variable.
The following code did't work.

getVectorX[n_]:=Table[Subscript[x, i], {i, 1, n}]
x = getVectorX

• if you need indexed variables it is better suggested to use x[i] instead of Subscript[x,i] – user42582 Sep 21 '18 at 8:44
• Thank you very much, user42582 for your advice. – tchappy ha Sep 21 '18 at 9:23
• $x = 1$, $x = 2$, Then $x$ returns $2$. But I want to use $x$ as a variable independent to $x$. – tchappy ha Sep 21 '18 at 9:28
• Array[Subscript[x, #] &, 10] but, as user42582 points out, indexed variables can cause problems (this is my experience). Maybe consider Array[x[#] &, 10]?. – user1066 Sep 21 '18 at 10:08
• @tchappyha you need to decide what symbol you are going to use for you variables eg x and then you should use a different symbol for the the list of variables you are going to create; using x for the variables (eg x) and the list of generated variables (eg x = {x, x,...}) causes infinite recursion; you could write something like eg y = {x, x, x}. – user42582 Sep 21 '18 at 11:22

It raises an error because x appears at both the right- and left-hand sides of your last expression. Try renaming one side instead, for example

xVec = getVectorX


BTW, Indexed can replace Subscript here.

• Thank you very much. – tchappy ha Sep 21 '18 at 9:21

There is no reason for the subscripted variable to be a symbol. Just use a string instead, and you won't have to worry about recursion errors because the subscripted variable is the same as the variable on the LHS:

getVectorX[n_] := Table[Subscript["x", i], {i, 1, n}]
x = getVectorX;
x //TeXForm


$$\left\{\text{x}_1,\text{x}_2,\text{x}_3,\text{x}_4,\text{x}_5\right\}$$