Skip to main content

list manipulation - Pick elements of largest absolute value


For example, given


list = {{1, -3, -5}, {2, 1, 6}, {0, 2, 4}, {-9, 2, 6}}

should return:


{-5, 6, 4, -9}


Updated


I found a undocumented function called Internal`MaxAbs,but it only accept two args,for example:


Internal`MaxAbs[2, -3]
Internal`MaxAbs[{1, -3, -5}, {2, 1, 6}]
(*
3
{2, 3, 6}
*)

How can I make it can accept multiple parameters?




Answer



(Edited with a slight tweak for a tiny bit more speed)


For the non-duplicate version I am finding this quite fast:


f = If[+## > 0, ##] & @@ {Max[#], Min[#]} &

f /@ list
(* {-5, 6, 4, -9} *)

Comments