TransWikia.com

Programmatically defining variables

Mathematica Asked on February 1, 2021

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]

One Answer

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

Answered by Bob Hanlon on February 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP