1
$\begingroup$

I have a list that reads

alist={{-0.7681699655, 0.7373960565, -0.7373960565, 0.7681699655}, {-0.7430781170, 0.7229609036, -0.6069296501, 0.6270468636}, {-0.5667102509, 0.5590489168, -0.2406532537, 0.2483145877}, {-0.1208579429, 0.1289608681, -0.07672268364,0.06861975842}, {-0.05781298049, 0.08931404647, -0.08206351097,0.05056244499}, {-0.03742013870, 0.05996910069, -0.05996910069,0.03742013870}, {-0.02094729733, -0.02965689326, 0.02838464090,0.02221954970}, {-0.0007664772180, -0.05347349201, 0.1208618572, -0.06662188802}, {0.3454536140, -0.3939473484, 0.6595390083, -0.6110452740}, {0.6342722531, -0.6745157548, 0.7845209706, -0.7442774690}, {-0.7681699655,0.7373960565, -0.7373960565, 0.7681699655}};

I would like to find two numbers from each pair of internal brackets that has the smallest absolute values. So that my output casts

minalist={{0.7373960565, -0.7373960565},{-0.6069296501, 0.6270468636}, ...}

I have tried using Min and MinimalBy[alist, Abs] but the results are for the whole list and not for each internal bracket. Do you have any suggestions?

$\endgroup$

1 Answer 1

3
$\begingroup$

Option 1: First define a function for a single list:

minabs2[expr_]:=MinimalBy[expr,Abs,2];

Then use Map to apply minabs2 to each element of the list, that is, each inner list:

Map[minabs2,alist]

Option 2: As a one-liner:

Map[MinimalBy[#,Abs,2]&,alist]
$\endgroup$
5
  • $\begingroup$ Great! Thanks! Is it also possible to Sort these data points for each internal bracket after performing Map? $\endgroup$
    – Shasa
    Jul 3 at 15:31
  • 1
    $\begingroup$ +1 To also Sort use Sort@*(MinimalBy[#, Abs, 2] &) /@ alist or Sort /@ (MinimalBy[#, Abs, 2] & /@ alist) $\endgroup$
    – Bob Hanlon
    Jul 3 at 15:43
  • $\begingroup$ Thank you @BobHanlon! $\endgroup$
    – Shasa
    Jul 3 at 15:47
  • 2
    $\begingroup$ If you want to sort by absolute value, then it turns out that MinimalBy[#,Abs,2]& already does that, as explained in the documentation. If you want to sort in the usual sense, see the comment of @BobHanlon. I recommend looking at the documentation of Map which is an extremely useful command once you know it. $\endgroup$
    – user293787
    Jul 3 at 16:01
  • $\begingroup$ Wonderful! Thanks for your suggestion! $\endgroup$
    – Shasa
    Jul 3 at 17:09

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.