# Select on 3D-table along diagonal

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?

• Use <= and >= , since none of your data points actually meet your criteria Oct 26 '15 at 13:34

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}}

• Thanks for introducing me to Cases, it looks more explicit than Select (it quickly becomes cumbersome I think) Oct 26 '15 at 13:36
• @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. Oct 26 '15 at 13:39