How can I select the highest root of the list? I was working to find only the root with the highest value.
{{B -> 0.622096}, {B -> 1.03445}, {B -> 48.6767}}
I want to apear only
{{B -> 48.6767}} or {B -> 48.6767}
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityHow can I select the highest root of the list? I was working to find only the root with the highest value.
{{B -> 0.622096}, {B -> 1.03445}, {B -> 48.6767}}
I want to apear only
{{B -> 48.6767}} or {B -> 48.6767}
Perfect case for MaximalBy
and the operator form of Lookup
.
roots = {{B -> 0.622096}, {B -> 1.03445}, {B -> 48.6767}};
MaximalBy[roots, Lookup[B]]
(* {{B -> 48.6767}} *)
MaximalBy[roots, Identity]
works too.
$\endgroup$
– Chip Hurst
Jun 26 '20 at 0:52
Try this:
expr = {{B -> 0.622096}, {B -> 1.03445}, {B -> 48.6767}};
B -> Max[Transpose[expr][[1]] /. Rule[B, x_] -> x]
(* B -> 48.6767 *)
Have fun!
I like the neat functionality of Ordering
:
Extract[#,Ordering[#,-1]]&@roots
{B->48.6767}
If you prefer Cases
:
Cases[#,List[Rule[b_,Max@Values@#]]]&@roots
{{B->48.6767}}
roots = {{B -> 0.622096}, {B -> 1.03445}, {B -> 48.6767}};
ClearAll[f1, f2, f3]
f1 = Merge[Max];
f2 = Last @* Sort;
f3 = #[[Ordering[#, -1]]] &;
f1 @ roots
<|B -> 48.6767|>
If you need a list:
Normal @ %
{B -> 48.6767}
f2 @ roots
{B -> 48.6767}
f3 @ roots
{{B -> 48.6767}}
Using TakeLargestBy
roots = {{B -> 0.622096}, {B -> 1.03445}, {B -> 48.6767}};
TakeLargestBy[roots, #[[-1, -1]] &, 1]
(* {{B -> 48.6767}} *)
Sort
andMaximum
. $\endgroup$ – David G. Stork Jun 25 '20 at 16:54