2
$\begingroup$

This question already has an answer here:

I've defined manually 32 elements as w1, w2, w3,..., w32. Is there a simple way to put them in a list rather that write B={w1, w2, ..., w32}? Like a list making function that understands some indexes? (here, each wi is itself a list, i=1,...,32 )

$\endgroup$

marked as duplicate by rhermans, Community Sep 27 '17 at 15:41

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 4
    $\begingroup$ Use indexed variables. B = Array[w, 32] $\endgroup$ – Bob Hanlon Sep 27 '17 at 15:03
  • $\begingroup$ @BobHanlon So I have to relabel the elements as w[1], w[2], etc. ? $\endgroup$ – Filburt Sep 27 '17 at 15:15
  • 2
    $\begingroup$ you could use Table[Symbol["w" <> ToString[i]], {i, 1, 32}] $\endgroup$ – RMMA Sep 27 '17 at 15:23
  • $\begingroup$ Yes. Use Edit | Find | Replace All $\endgroup$ – Bob Hanlon Sep 27 '17 at 15:58
3
$\begingroup$

You can use Names[x] to get all the symbols that match the string or pattern argument.

If you want the names

B = Names["w*"]

or if you want the content you can use Symbol or ToExpression

B = ToExpression /@ Names["w*"]

But ultimately the answer is to use indexed variables as noted by Bob Hanlon.

Read the documentation and this Q&A that comments of the use of string-based variables names, as the comment by RMMA.

$\endgroup$

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