1
$\begingroup$

I seems as though what I am interested in is unconventional, but I am interested in defining a number of different variables programmatically.

For example I can write a matrix of variables by doing:

Table[Symbol["\[Rho]" <> ToString@i <> ToString@j], {i, 5}, {j, 
   5}] // MatrixForm

But is it impossible for me to assign specific values to them?

Here's one of my attempts that doesn't work:

For[i = 0, i < 3, i++, Symbol["fish" <> ToString@i] = 3]
$\endgroup$

1 Answer 1

2
$\begingroup$
Clear["Global`*"]

It is much easier to use an indexed variable and use Format to display the variable any way that you want.

For example, to display μ as a subscripted variable

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

then

var = Array[μ, 5]

enter image description here

Assigning values is straightforward:

μ[1] = μ[4] = 3;

The result is

var

enter image description here

$\endgroup$
3
  • $\begingroup$ Thank you for your answer. While I like the recommendation of switching to the subscript formatting, does this completely answer the question for assigning values programmatically? It works for the for loop I specified, but I'm not sure it would work if I chose a more complicated example. (for example, what about: For[i = 0, i < 3, i++, Symbol["fish" <> ToString@i] = 3i]?) $\endgroup$ Commented Dec 4, 2020 at 1:44
  • $\begingroup$ (fish[#] = 3 #) & /@ Range[0, 2] then look at say fish[2] $\endgroup$
    – Bob Hanlon
    Commented Dec 4, 2020 at 1:50
  • $\begingroup$ Interesting. Thanks a lot! $\endgroup$ Commented Dec 4, 2020 at 1:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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