# Select 2D data in a certain range

I have the following data points:

SeedRandom[1];

array = RandomReal[1, {1000, 2}];

ListPlot[array, Frame -> True, FrameLabel -> {"x", "y"}]


a) How can I select the data points in the range x = [0.4, 0.7]?

b) What would be the solution if I need the data in the rectangle: x = [0.4, 0.7] and y = [0.2, 0.6]?

You can use Select:

a)

ListPlot[#, Frame -> True, FrameLabel -> {"x", "y"},
PlotRange -> {{0, 1}, {0, 1}}]& @
Select[array, 0.4 < #[[1]] < 0.7 &]


b)

ListPlot[#, Frame -> True, FrameLabel -> {"x", "y"},
PlotRange -> {{0, 1}, {0, 1}}]& @
Select[array, 0.4 < #[[1]] < 0.7 && 0.2 < #[[2]] < 0.6 &]


• Thank you for this solution ... – lio Nov 8 '16 at 13:41

Using ImplicitRegion to define regions

SeedRandom[1];

array = RandomReal[1, {1000, 2}];

rgn1 = ImplicitRegion[0.4 <= x <= 0.7, {x, y}];
rgn2 = ImplicitRegion[0.4 <= x <= 0.7 &&
0.2 <= y <= 0.6, {x, y}];

ListPlot[
{array, Select[array, # ∈ rgn1 &]},
PlotStyle -> {
Directive[LightGray, PointSize[Small]],
Directive[Red, PointSize[Medium]]},
Frame -> True,
FrameLabel -> (Style[#, 14, Bold] & /@ {"x", "y"}),
ImageSize -> Medium]


ListPlot[
{array, Select[array, # ∈ rgn2 &]},
PlotStyle -> {
Directive[LightGray, PointSize[Small]],
Directive[Red, PointSize[Medium]]},
Frame -> True,
FrameLabel -> (Style[#, 14, Bold] & /@ {"x", "y"}),
ImageSize -> Medium]


• How can we select numbers from a matrix (10, 10) in a range? I see that Boole[] can be used to select numbers in a range using list but I could not figure out how to use Boole[] if I need to use a matrix instead of a list of numbers. Do you how? – Tugrul Temel Jan 29 at 16:30
• Post a question and clarify what you are trying to do with a concrete example. What is the expected output? Are you trying to randomly select values from the matrix that meets some criteria? With or without replacement? Are you looking for a list (vector) of all values in the matrix that meet the criteria? Are you looking for the result to be the (perhaps ragged) matrix resulting from eliminating values from the original matrix that do not meet the criteria? – Bob Hanlon Jan 29 at 16:50