4
$\begingroup$

I have the following data points:

SeedRandom[1];

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

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

enter image description here

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]?

$\endgroup$
11
$\begingroup$

You can use Select:

a)

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

enter image description here

b)

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

enter image description here

$\endgroup$
1
  • $\begingroup$ Thank you for this solution ... $\endgroup$ – lio Nov 8 '16 at 13:41
10
$\begingroup$

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]

enter image description here

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

enter image description here

$\endgroup$
2
  • $\begingroup$ 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? $\endgroup$ – Tugrul Temel Jan 29 at 16:30
  • 1
    $\begingroup$ 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? $\endgroup$ – Bob Hanlon Jan 29 at 16:50

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.