1
$\begingroup$

I have the following data set on the form (x, y, z)

data = {
   {1, 1.1, 3},
   {2, 3, 5},
   {2.5, 4, 5},
   {4, 5, 6},
   {4.1, 4, 6},
   {100, 100, 6}};

I would like the data along the diagonal x = y ± δ, where δ = 0.1 (as an example). I tried using this Select-statement,

ε = 0.1;
Select[data, -ε + #[[1]] < #[[2]] < +ε + #[[1]] &]

but that merely outputs {{100, 100, 6}}, which I don't seem to understand. Is Select the best method for extracting/filtering data like this and is it even possible?

$\endgroup$
  • 1
    $\begingroup$ Use <= and >= , since none of your data points actually meet your criteria $\endgroup$ – Jason B. Oct 26 '15 at 13:34
4
$\begingroup$

Well, the reason for it not working is because you're using Less where you should be using LessEqual (if you expect to see more points).

In[1]:= Select[data, -ε + #[[1]] <= #[[2]] <= +ε + #[[1]] &]
Out[1]= {{1, 1.1, 3}, {4.1, 4, 6}, {100, 100, 6}}

That being said, I prefer using Cases and pattern matching for picking out elements from a list when I have to destructure the points to see if the match my criteria, since it allows you to be more explicit about what the various sub-parts mean. Also, use Abs!

In[2]:= Cases[data, {x_, y_, _} /; Abs[x - y] <= ε]
Out[2]= {{1, 1.1, 3}, {4.1, 4, 6}, {100, 100, 6}}
$\endgroup$
  • $\begingroup$ Thanks for introducing me to Cases, it looks more explicit than Select (it quickly becomes cumbersome I think) $\endgroup$ – BillyJean Oct 26 '15 at 13:36
  • 2
    $\begingroup$ @Billy, it's not about explicitness per se; altho almost anything expressed in one can be expressed in terms of the other, there are situations where using Select[] to pick elements of a list results in something tidier, and there are situation where Cases[] looks better than Select[]. Both have their place. $\endgroup$ – J. M. will be back soon Oct 26 '15 at 13:39

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.