# Filtering data 'geometrically'

I was looking at a very nice answer here, where the question was applying a confidence region to some data.

If we plot some data as a scatter plot:

XData = RandomVariate[NormalDistribution[1,1], 100000];
YData = RandomVariate[NormalDistribution[1,1], 100000];

XYDATA = Transpose[{XData, YData}];

XYCoVarMat = Covariance[XYDATA];
XYMean = {Mean[XData], Mean[YData]};

ListPlot[
XYDATA, Frame->True, FrameLabel->{"X","Y"}, LabelStyle->16, AspectRatio->1,
Epilog -> {
Opacity[0.32, Green], EdgeForm[{Green, AbsoluteThickness[1]}], Ellipsoid[XYMean, 4 XYCoVarMat],
Opacity[0.32, Blue], EdgeForm[{Blue, AbsoluteThickness[1]}], Ellipsoid[XYMean, 3 XYCoVarMat],
Opacity[0.32, Red], EdgeForm[{Red, AbsoluteThickness[1]}], Ellipsoid[XYMean, 2 XYCoVarMat],
Opacity[0.32, Yellow], EdgeForm[{Yellow, AbsoluteThickness[1]}], Ellipsoid[XYMean, 1 XYCoVarMat]
},
PlotRange->{{-8,8},{-8,8}}
]


I was wondering if anyone knew a way of filtering the data using this approach for example only select data that is with the ellipsoid boundary of Ellipsoid[XYMean, 1 XYCoVarMat]?

• Would With[{rmf = RegionMember[Ellipsoid[XYMean, 1 XYCoVarMat]]}, Select[XYDATA, rmf]] work for you? – J. M. will be back soon Nov 15 at 16:09
• It would work wonderfully. I'm starting to wonder if you are actually written purely from Mathematica functions. Thanks! – Q.P. Nov 15 at 16:14
• @J.M.willbebacksoon I don't know if you want to post an answer yourself or not, but given you provided the solution I feel you should get credit. If credit doesn't concern you I will accept ThatGravityGuy's answer so that the question is completed and helps users who find this question. – Q.P. Nov 16 at 13:15
• Please accept ThatGravityGuy's answer if you think you found it useful. – J. M. will be back soon Nov 16 at 13:24

As @J.M. pointed out in the comments, the key here is the function RegionMember. Since you already know the region that you want to work with, namely Ellipsoid[XYMean, 1 XYCoVarMat], you can use Select as
With[{rmf = RegionMember[Ellipsoid[XYMean, 1 XYCoVarMat]]}, Select[XYDATA, rmf]]