5
$\begingroup$

I have the following array of data. Suppose I want only to keep data within the two lines. I can achieve my result using the following code:

data = Table[{i, RandomReal[{-10, 10}]}, {i, 0, 100}];
line1[x_] := 0.1 x - 10;
line2[x_] := -0.1 x + 10;

enter image description here

rf = RegionMember[Polygon[{{0, line1[0]}, {0, line2[0]}, {100,line1[100]}}]];
bool1 = rf[data];
notbool1 = Not[#] & /@ bool1
datain = Pick[data, bool1];
dataout = Pick[data, notbool1];

This has the desired effect.

enter image description here

Now suppose I only have one line, since I can't make a closed region what would be the best way to select points either above or below a line? What if the curve is not a straight line? Any ideas?

$\endgroup$
5
$\begingroup$

Some knowledge about (planar) analytic geometry facilitates this problem. Suppose now you only have line1, this

Select[data, line1[ #[[1]] ] < #[[2]] &]

selects out the points above line1; the key lies in the inequality. If it is connected by >, then the points below line1 you will get.

$\endgroup$
3
$\begingroup$
{in, out} = GeneralUtilities`SelectDiscard[data, line1[#[[1]]]<=#[[2]]<=line2[#[[1]]]&];

Show[ListPlot[{in, out}, PlotStyle -> {Green, Red}], 
 Plot[{line1@x, line2@x}, {x, 0, 100}, 
  Filling -> {1 -> {{2}, {Opacity[.5, LightBlue], None}}}]] 

enter image description here

{above1, below1} = GeneralUtilities`SelectDiscard[data, line1[#[[1]]] <= #[[2]] &];
{above2, below2} = GeneralUtilities`SelectDiscard[data, line2[#[[1]]] <= #[[2]] &]; 

{plt1, plt2} = Show[ListPlot[#, PlotStyle -> {Green, Red}], 
      Plot[{line1@x, line2@x}, {x, 0, 100}, 
        Filling -> {1 -> {{2}, {Opacity[.5, LightBlue], None}}}]] & /@ 
  {{above1,below1}, {above2, below2} };
Row[{plt1, plt2}, Spacer[5]]

enter image description here

     

$\endgroup$

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.