Efficiently select the smallest magnitude element from each column of a matrix
m =
{{-351, -260, -148, -159, 1},
{-197, -106, 6, -5, 155},
{-194, -103, 9, -2, 158},
{-104, -13, 99, 88, 248},
{28, 119, 231, 220, 380}};
Lets not forget that Abs
is Listable
, so we only have to map Min
.
Min /@ Abs[Transpose[m]]
{28, 13, 6, 2, 1}
This
mat = {{-351, -260, -148, -159, 1},
{-197, -106, 6, -5, 155},
{-194, -103, 9, -2, 158},
{-104, -13, 99, 88, 248},
{ 28, 119, 231, 220, 380}};
Map[Min[Abs[#]] &, Transpose[mat]]
finds the minimum absolute value element in each column {28, 13, 6, 2, 1}
You can do the same thing without needing to understand #
and &
by
findminabs[v_] := Min[Abs[v]];
Map[findminabs, Transpose[mat]]
You can also use Composition
(@*
)
Min @* Abs /@ Transpose[mat]
{28, 13, 6, 2, 1}
To retain the signs:
MinimalBy[Abs] /@ Transpose[mat] // Flatten
{28, -13, 6, -2, 1}