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.
-
$\begingroup$ related: 33184 $\endgroup$– Kuba ♦Jun 18, 2014 at 8:54
2 Answers
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 ??Global`A*, 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]:= ?? Global`A*
Global`
A1 A2 A3 A4 A5 A6 A7 A8
But I guess I'm not answering your question. Sorry.
-
$\begingroup$ Dear user15996, we are very glad you found the site. Thank you for your contribution. Unfortunately I can't see how this answers the question. $\endgroup$– VerbeiaJun 18, 2014 at 11:26