9
$\begingroup$

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}
$\endgroup$
  • $\begingroup$ See Sort and Maximum. $\endgroup$ – David G. Stork Jun 25 '20 at 16:54
10
$\begingroup$

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}} *) 
$\endgroup$
  • 2
    $\begingroup$ Or MaximalBy[roots, Last] $\endgroup$ – Bob Hanlon Jun 26 '20 at 0:32
  • 2
    $\begingroup$ In this case even MaximalBy[roots, Identity] works too. $\endgroup$ – Chip Hurst Jun 26 '20 at 0:52
  • 2
    $\begingroup$ Or MaximalBy[roots, Values]. $\endgroup$ – chyanog Jun 26 '20 at 3:21
7
$\begingroup$
roots /. soln // Max

Works and uses core functions.

$\endgroup$
4
$\begingroup$

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!

$\endgroup$
4
$\begingroup$

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}}

$\endgroup$
3
$\begingroup$
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}}
$\endgroup$
2
$\begingroup$

Using TakeLargestBy

roots = {{B -> 0.622096}, {B -> 1.03445}, {B -> 48.6767}};

TakeLargestBy[roots, #[[-1, -1]] &, 1]
(* {{B -> 48.6767}} *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.