1
$\begingroup$

I have a list from which I want to extract only the values beginning from where b (from {a,b}) is the closest to zero. For example:

I want to extract

data={{2, -0.036704}, {56.7499, 0.95489}, {73.7349, 0.73489}, {2, -0.237504}}

from the following list:

list = {{1, 0.67342}, {77.7499, -0.93489}, {6.73448, 0.056704}, {2, -0.036704}, 
 {56.7499, 0.95489}, {73.7349, 0.73489}, {2, -0.237504}}

Thanks in advance

$\endgroup$
1
  • 1
    $\begingroup$ Look up Nearestin the docs. $\endgroup$
    – Yves Klett
    Commented Nov 10, 2013 at 18:22

2 Answers 2

0
$\begingroup$

One way is to find the index ind where the minimum occurs, and then take all the remaining elements of the list:

ind = First@Ordering[First@Abs[Rest[Transpose[list]]]];
list[[ind ;;]]
{{2, -0.036704}, {56.7499, 0.95489}, {73.7349, 0.73489}, {2, -0.237504}}
$\endgroup$
1
  • $\begingroup$ For long lists, the following may be faster ind = First@ Ordering[Abs@ list[[All, 2]], 1] $\endgroup$
    – user8074
    Commented Nov 10, 2013 at 19:19
1
$\begingroup$

An alternative to find the starting index is to use Nearest:

ind = First @ Nearest[Last @ Transpose @ list -> "Index", 0];
list[[ind;;]]

{{2, -0.036704}, {56.7499, 0.95489}, {73.7349, 0.73489}, {2, -0.237504}}

For larger datasets, Nearest is faster:

data = RandomReal[{-100, 100}, {10^6, 2}];
second = Last @ Transpose @ data;

r1 = First @ Nearest[second -> "Index", 0] //RepeatedTiming
r2 = First @ Ordering[Abs @ second, 1] //RepeatedTiming 

{0.0018, 725110}

{0.0062, 725110}

$\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.