Optimization: list manipulation
ClearAll[f]
f = MapIndexed[If[LessEqual @@ Abs @ #,
{Symbol["x" <> ToString[#2[[1]]]], Sign @ Last @ #},
{Sign @ First @ #, Symbol["x" <> ToString[#2[[1]]]]}] &];
list = {{-5, 1}, {7, -3}, {4, 4}};
f @ list
{{-1, x1}, {1, x2}, {x3, 1}}
Also
symbolpositions = MapIndexed[Flatten[{#2, #}] &][2 - Boole[LessEqual @@@ Abs[list]]];
symbols = Symbol["x" <> ToString[#]] & /@ Range[Length@list];
ReplacePart[Sign@list, Thread[symbolpositions -> symbols]]
{{-1, x1}, {1, x2}, {x3, 1}}
MapIndexed
should be the suitable function working at the overall level. A version slightly different from that of @kglr 's, with OrderingBy
, ToExpression
, and StringTemplate
symbol = ToExpression[StringTemplate["x``"] @@ #] &;
func = If[Greater @@ OrderingBy[#, Abs],
{Sign[#[[1]]], symbol[#2]},
{symbol[#2], Sign[#[[2]]]}
] &;
list = {{-5, 1}, {7, -3}, {4, 4}};
MapIndexed[func][list]
{{-1, x1}, {1, x2}, {x3, 1}}