3
$\begingroup$

This code is to produce the list data from which I want to select some elements:

SeedRandom[1];
n = 10;
xmin = RandomInteger[{0, 20}, n]; 
ymin = RandomInteger[{0, 20}, n];
xmax = xmin + RandomInteger[{1, 10}, n];
ymax = ymin + RandomInteger[{1, 10}, n];

data = 
 Transpose[{xmin, ymin, xmax, ymax}] 
   /. {xmin_, ymin_, xmax_, ymax_} :> {{xmin, ymin}, {xmax, ymax}}  

{{{5, 3}, {11, 6}}, {{0, 8}, {5, 12}}, {{7, 19}, {11, 29}}, 
 {{0, 5}, {1, 11}}, {{2, 18}, {4, 20}}, {{3, 16}, {7, 22}}, 
 {{0, 12}, {6, 15}}, {{0, 0}, {4, 4}}, {{16, 19}, {17, 29}}, 
 {{14, 4}, {18, 6}}}

ydiff = data[[All, 2, 2]] - data[[All, 1, 2]]

{3, 4, 10, 6, 2, 6, 3, 4, 10, 2}

How can I select all data values where ydiff = (ymax - ymin) is for example greater than ydiffmin = 2 and less ydiffmax = 9?

The result should be:

{{{5, 3}, {11, 6}}, {{0, 8}, {5, 12}}, {{0, 5}, {1, 11}}, 
 {{3, 16}, {7, 22}}, {{0, 12}, {6, 15}}, {{0, 0}, {4, 4}}}
$\endgroup$
6
$\begingroup$
Select[data, 2 < #[[2, 2]] - #[[1, 2]] < 9 &](* or *)
Pick[data, 2 < # < 9 & /@ ydiff] 

all give

{{{5, 3}, {11, 6}}, {{0, 8}, {5, 12}}, {{0, 5}, {1, 11}}, {{3, 16}, {7, 22}}, {{0, 12}, {6, 15}}, {{0, 0}, {4, 4}}}

$\endgroup$
2
$\begingroup$

Szabolcs' BoolEval package comes to mind...

Needs["BoolEval`"]
BoolPick[data, 2 < ydiff < 9]

{{{5, 3}, {11, 6}}, {{0, 8}, {5, 12}}, {{0, 5}, {1, 11}}, {{3,
16}, {7, 22}}, {{0, 12}, {6, 15}}, {{0, 0}, {4, 4}}}

$\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.