# Calling variables with use of numbers in their names

I have got few huge matrices named by ascending numbers, e.g. x1, x2, x3,... and I need to do the same operation with all of them - to multiply their components, obtain the absolute value etc. As I am beginner with Mathematica I usually copy the expression and rewrite indexes as x1 -> x2, but this is very lengthy. I would like to have only one expression with something like xX, where I just change X by index of particular matrix.

• related: 33184
– Kuba
Commented Jun 18, 2014 at 8:54

f @ Symbol["x" <> ToString[#]] & /@ Range[10]

{f[x1], f[x2], f[x3], f[x4], f[x5], f[x6], f[x7], f[x8], f[x9], f[x10]}


e.g.

x1 = RandomReal[{-1, 1}, {5, 5}];
x2 = RandomReal[{-1, 1}, {5, 5}];

Composition[
Abs,
Tr,
Flatten,
Symbol["x" <> ToString[#]] &

] /@ Range[2]


Less sophisticated approach. The ?? at the end shows that Mathematica knows about these eight symbols. If I click on the "light blue" A1 in the Global symbol table that appears in my notebook after entering ??GlobalA*, the 16 elements of A1 are shown as a list of lists.

    In[1]:= Table[Symbol["A" <> ToString[i]], {i, 8}]

Out[1]= {A1, A2, A3, A4, A5, A6, A7, A8}

(* All of the symbols in the above list can be set to have numerical values *)

(* and manipulated as usual.  For example: *)

In[2]:= A1 = RandomReal[{-10.0, 10.0}, {4, 4}]

Out[2]= {{-5.5598, 9.8372, -0.700913, 2.89629},
{-6.42822, 1.26682, -7.52955, 3.2075},
{0.28862, -7.03238, 0.366672, 9.61496},
{6.60329, -8.92156, -8.83749, -7.38917}}

In[3]:= A1.Inverse[A1] // Chop

Out[3]= {{1., 0, 0, 0},
{0, 1., 0, 0},
{0, 0, 1., 0},
{0, 0, 0, 1.}}

In[4]:= ?? GlobalA*

Global
A1   A2   A3   A4   A5   A6   A7   A8