2
$\begingroup$

I am trying to do something which I assume should be very simple: I want to generate a list with symbolic coefficients that looks like this:

A = {a0, a1, a2, a3, ... , a20};

And I mean to do this a lot up to an arbitrary number, so I wouldn't want to type them all. So of course when I set one of them, say a1 = 5, then it should be updated in the list, so now:

A = {a0, 5, a2, a3, ... , a20};

I might just be missing an obvious Mathematica command here that does this... Is there a way to do such a thing?

$\endgroup$
3
  • 1
    $\begingroup$ You can use Symbol["a" <> ToString[#]] & /@ Range[20]. $\endgroup$ Commented Apr 9, 2013 at 12:49
  • 1
    $\begingroup$ Thanks, that exactly the line that I needed! (except that I used Range[0,20] as I start at 0, but never mind that) $\endgroup$
    – Jelle
    Commented Apr 9, 2013 at 13:16
  • $\begingroup$ Other possible duplicates: this $\endgroup$ Commented Apr 9, 2013 at 13:38

1 Answer 1

5
$\begingroup$

Depending on the problem you are having, it is maybe enough when you create a list of {a[0], a[1], a[2], a[3], ...}. This can be done with

Array[a, 10]

or, if you like Table

Table[a[i], {i, 10}]

If you really want to create an where n is a number, then you can use Symbol to convert a string to a symbol

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

or, if you want definitely unassigned variables and you don't care which numbers they get

Table[Unique["a"], {10}]
$\endgroup$
4
  • $\begingroup$ Ah, thank you very much! The Symbol[] command was just what I needed! $\endgroup$
    – Jelle
    Commented Apr 9, 2013 at 13:13
  • $\begingroup$ What about also mentioning ToExpression? $\endgroup$
    – Yves Klett
    Commented Apr 9, 2013 at 14:19
  • $\begingroup$ @YvesKlett I assume it will be closed as dup soon and in the other post ToExpression is used. $\endgroup$
    – halirutan
    Commented Apr 9, 2013 at 15:30
  • $\begingroup$ Yup. I voted for that but I think your answer is nice, short and useful anyway. $\endgroup$
    – Yves Klett
    Commented Apr 9, 2013 at 15:57

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