6
$\begingroup$

I need, given "n", create a list {a1,a2,...,an}. For example: Given n=3, the list that must be created is composed by the symbolic variables {a1,a2,a3}. Sorry if this question is simple. Does anybody knows how to do it? Thanks.

$\endgroup$
  • 6
    $\begingroup$ Use e.g. Array[a, 3]. This will give you indexed variables which should serve the same purpose as those you look for. $\endgroup$ – MarcoB Jun 8 '18 at 18:00
  • 1
    $\begingroup$ "a" <> ToString[#] & /@ Range[10] or Table["a" <> IntegerString[i, 10], {i, 1, 10}] $\endgroup$ – user1066 Jun 9 '18 at 0:01
8
$\begingroup$

For example, for n=5

Table[Symbol["a" <> ToString[i]], {i, 5}]

but consider that in any case it may not be optimal for what you need to do. If you want to share what you are planning to use this list for, we may help you with a better construct.

$\endgroup$
  • 1
    $\begingroup$ Thanks very much Fraccalo. Helped quite a lot. I am trying to manipulate equations based on Feynman diagrams. But I won't bother you with this. You have just helped me a lot. Thank You again. Best wishes, J.R.Descardeci $\endgroup$ – Jose Ricardo Descardeci Jun 8 '18 at 18:17
5
$\begingroup$

You might also try with:

Table[Unique["a"], {n}]

The advantage and disadvantage of this is that generated variables are unique - there will be no conflict with but also no reuse of existing ones.

Note: depending on what you do, you might also want to Remove such generated variables after they are no longer needed, to avoid leaking memory for unused symbols.

$\endgroup$
2
$\begingroup$

As stated by MarcoB, indexed variables are highly convenient and less prone to error or difficulties. They can be displayed in any manner that you desire using Format.

n = 5;

list = Array[a, n];

To display as you show

Format[a[n_]] := Symbol["a" <> ToString[n]]

list

enter image description here

To display as subscripts

Format[a[n_]] := Subscript[a, n]

list

enter image description here

Or with a specified style

Format[a[n_]] := Style[Subscript[a, n], Bold, Italic, Blue]

list

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.