3
$\begingroup$

Might the DeleteCases have issues in matching when the list is of floats (machine precision numbers)? E.g. in this code it might fail to match correctly because float==float is not a good operation:

ClearAll[a];
a = RandomReal[{0, 1}, 5]
DeleteCases[a, Max[a]]

How would you handle this type of situations? What is the robust way?

$\endgroup$
3
  • 5
    $\begingroup$ In the posted example it will not fail because Max[a] is a member of a so it must match. $\endgroup$ Commented Mar 29, 2020 at 22:49
  • $\begingroup$ But might there be rounding issues somewhere in the Max that would cause the matching to fail? $\endgroup$
    – Al Guy
    Commented Mar 29, 2020 at 23:12
  • $\begingroup$ No issues in this example. But if there were, the robust method is to allow some appropriate epsilon plus/minus. $\endgroup$ Commented Mar 30, 2020 at 14:28

1 Answer 1

6
$\begingroup$

A slightly faster method (30x for large lists) is to use Ordering and Drop:

Drop[a, Ordering[a, -1]]

This will always remove 1 element, even in the extreme rare case that the maximum value appears twice…

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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