( Did not readily find from Documentation even if used before).
NSolve[ 2 x^3 + x ^2 - 5 x + 2 == 0, x]
How to collect roots with new labels a,b and c?
( Did not readily find from Documentation even if used before).
NSolve[ 2 x^3 + x ^2 - 5 x + 2 == 0, x]
How to collect roots with new labels a,b and c?
sol = NSolve[2 x^3 + x^2 - 5 x + 2 == 0, x]
(* {{x -> -2.}, {x -> 0.5}, {x -> 1.}} *)
Judging from OP's acceptance of this answer, the task is turn sol
into
{{a -> -2.}, {b -> 0.5}, {c -> 1.}}
Here are a few methods, sorted by StringCount
, to relabel x
new = {a, b, c};
List /@ Thread[new -> (x /. sol)]
List /@ Thread[new -> Last @@@ sol]
Block[{i = 1}, sol /. x :> new[[i++]]]
List /@ Thread[new -> sol[[All, 1, 2]]]
List /@ MapThread[Rule, {new, x /. sol}]
Block[{i = 1}, sol /. Thread[x :> new[[i++]]]]
Block[{sol2 = sol}, sol2[[All, 1, 1]] = new; sol2]
Block[{i = 1}, sol /. Rule[x, y_] :> Rule[new[[i++]], y]]
Block[{nl = Reverse@new}, sol /. x :> First[nl = RotateRight[nl]]]
Block[{nl = Reverse@new}, Replace[sol, x :> First[nl = RotateRight[nl]], 3]]
Block[{nl = Reverse@new}, MapAt[First[nl = RotateRight[nl]] &, sol, {{All, 1, 1}}]]
Block[{nl = Reverse@new},ReplacePart[sol, {_, 1, 1} :> First[nl = RotateRight[nl]]]]
all give
(* {{a -> -2.}, {b -> 0.5}, {c -> 1.}} *)
Thread[{a, b, c} -> (x /. sol)]
orThread[{a, b, c} -> Last @@@ sol]
? $\endgroup$ – kglr Apr 14 '15 at 14:52{{x -> -2.}, {x -> 0.5}, {x -> 1.}}
into{{a -> -2.}, {b -> 0.5}, {c -> 1.}}
. None of the answers in the linked q/a does that. So I suggest this is not a duplicate. $\endgroup$ – kglr Apr 14 '15 at 20:22