Mathematica Asked on September 3, 2021
I have:
list = {{1, AGR}, {2, AUS}, {3, AUT}};
but I want the following using Table[.]
.
mmARG = mmB[1];
mmAUS = mmB[2];
mmAUT = mmB[3];
Any idea?
I am not sure if this will do what you want. But it generates what you show.
Clear["Global`*"]
list = {{1, AGR}, {2, AUS}, {3, AUT}};
tbl = MapIndexed[ToExpression[("mm" <> ToString[#1[[2]]])] ==
ToExpression["mmB[" <> ToString@First[#2] <> "]"] &, list];
TableForm[tbl]
If you wan Set
istead of Equal
, change the above to
list = {{1, AGR}, {2, AUS}, {3, AUT}};
tbl = MapIndexed[{ToExpression[("mm" <> ToString[#1[[2]]])],
ToExpression["mmB[" <> ToString@First[#2] <> "]"]} &, list];
Set @@@ tbl
And now
etc...
If you want to use Set
for display, then evaluate it later, you can do
list = {{1, AGR}, {2, AUS}, {3, AUT}};
tbl = MapIndexed[("mm" <> ToString[#1[[2]]] <> " = " <> "mmB[" <>
ToString@First[#2] <> "]") &, list];
TableForm[tbl]
But the above is string. To Make it actual Mathematica Set
, need to use
ToExpression[tbl]
And now the assignment is made.
Answer comment
making the same operation with the following
You could try
Clear["Global`*"]
list = {{1, AGR}, {2, AUS}, {3, AUT}};
tbl = MapIndexed[("mm" <> ToString[#1[[2]]] <> " = " <> "mmB[" <>
ToString@First[#2] <> "] - DiagonalMatrix[Diagonal[mmB[" <>
ToString@First[#2] <> "]]]") &, list]
TableForm[tbl]
Which gives
But before applying ToExpression
to the above, you would need to have those mmB[i]
defined, else Diagonal
will give error. It needs them to be lists.
Something like
mmB[1] = {1, 2, 3};
mmB[2] = {4, 5, 6};
mmB[3] = {7, 8, 9};
And only now you can do ToExpression[tbl]
Correct answer by Nasser on September 3, 2021
I think it's generally not a good idea to encode information in the variable name. Much more natural in Mathematica would be to encode the left-hand side as a pattern:
list = {{1, AGR}, {2, AUS}, {3, AUT}};
(mm[#[[2]]] = mmB[#[[1]]]) & /@ list;
The information is now stored as
?mm
(* mm[AGR] = mmB[1]
mm[AUS] = mmB[2]
mm[AUT] = mmB[3] *)
so the left-hand sides are mm[AGR]
instead of your mmAGR
. You can treat mm[AGR]
as a regular variable.
The extended example works in the same way,
(mm[#[[2]]] = mmB[#[[1]]] - DiagonalMatrix[Diagonal[mmB[#[[1]]]]]) & /@ list;
Answered by Roman on September 3, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP