4
$\begingroup$

I need to make a fit on a small part of my data list. To do so I need to extract data setting a condition.

Example: From the list below I would like to create a new list that contains only data whose first element (my x variable) is in between -0.4096 and -0.4089

datalist = {{-0.4096, 0.15332}, {-0.40955, 0.13269}, {-0.4095, 0.175537}, {-0.40945, 0.130859},
            {-0.4094, 0.157714}, {-0.40935, 0.121826}, {-0.4093, 0.137207},
            {-0.40925, 0.135009}, {-0.4092, 0.118774}, {-0.40915, 0.126831},
            {-0.4091, 0.127441}, {-0.40905, 0.14392}, {-0.409, 0.145507},
            {-0.40895, 0.16455}, {-0.4089, 0.145141}, {-0.40885, 0.138305},
            {-0.4088, 0.141845}, {-0.40875, 0.145874}, {-0.4087, 0.125122},
            {-0.40865, 0.140747}, {-0.4086, 0.124145}, {-0.40855, 0.146362},
            {-0.4085, 0.139892}}

Take in mind that my actual data lists are much longer that the above one.

Thank you very much for your help!

Alberto

$\endgroup$
2
  • $\begingroup$ try Select[-0.4089<=#[[1]]<=-0.4096&]@datalist $\endgroup$
    – kglr
    Commented May 28, 2020 at 8:56
  • $\begingroup$ Your Example: paragraph actually contains all the Wolfram Language symbols of the solution you are looking for: datalist // Select[First /* Between[{-0.4089, -0.4096}]]. $\endgroup$ Commented Feb 20 at 0:55

5 Answers 5

2
$\begingroup$
datalist = {{-0.4096, 0.15332}, {-0.40955, 0.13269}, {-0.4095, 
    0.175537}, {-0.40945, 0.130859}, {-0.4094, 0.157714}, {-0.40935, 
    0.121826}, {-0.4093, 0.137207}, {-0.40925, 0.135009}, {-0.4092, 
    0.118774}, {-0.40915, 0.126831}, {-0.4091, 0.127441}, {-0.40905, 
    0.14392}, {-0.409, 0.145507}, {-0.40895, 0.16455}, {-0.4089, 
    0.145141}, {-0.40885, 0.138305}, {-0.4088, 0.141845}, {-0.40875, 
    0.145874}, {-0.4087, 0.125122}, {-0.40865, 0.140747}, {-0.4086, 
    0.124145}, {-0.40855, 0.146362}, {-0.4085, 0.139892}};

Try this:

Select[datalist, -0.496 <= #[[1]] <= 0.4089 &]

(* {{-0.4096, 0.15332}, {-0.40955, 0.13269}, {-0.4095, 
  0.175537}, {-0.40945, 0.130859}, {-0.4094, 0.157714}, {-0.40935, 
  0.121826}, {-0.4093, 0.137207}, {-0.40925, 0.135009}, {-0.4092, 
  0.118774}, {-0.40915, 0.126831}, {-0.4091, 0.127441}, {-0.40905, 
  0.14392}, {-0.409, 0.145507}, {-0.40895, 0.16455}, {-0.4089, 
  0.145141}, {-0.40885, 0.138305}, {-0.4088, 0.141845}, {-0.40875, 
  0.145874}, {-0.4087, 0.125122}, {-0.40865, 0.140747}, {-0.4086, 
  0.124145}, {-0.40855, 0.146362}, {-0.4085, 0.139892}}   *)

or this

datalist /. {x_, y_} /; x < -0.496 || x > -0.4089 -> Nothing

(* {{-0.4096, 0.15332}, {-0.40955, 0.13269}, {-0.4095, 
  0.175537}, {-0.40945, 0.130859}, {-0.4094, 0.157714}, {-0.40935, 
  0.121826}, {-0.4093, 0.137207}, {-0.40925, 0.135009}, {-0.4092, 
  0.118774}, {-0.40915, 0.126831}, {-0.4091, 0.127441}, {-0.40905, 
  0.14392}, {-0.409, 0.145507}, {-0.40895, 0.16455}, {-0.4089, 
  0.145141}} *)

Have fun!

$\endgroup$
1
  • $\begingroup$ Thanks!!! Both work exactly as I wanted $\endgroup$
    – alpor
    Commented May 28, 2020 at 9:09
2
$\begingroup$

Another possibility using Pick:

Pick[datalist,Unitize[Clip[datalist[[All,1]], {-0.4096, -0.4089},{0,0}]],1]

{{-0.4096, 0.15332}, {-0.40955, 0.13269}, {-0.4095, 0.175537}, {-0.40945, 0.130859}, {-0.4094, 0.157714}, {-0.40935, 0.121826}, {-0.4093, 0.137207}, {-0.40925, 0.135009}, {-0.4092, 0.118774}, {-0.40915, 0.126831}, {-0.4091, 0.127441}, {-0.40905, 0.14392}, {-0.409, 0.145507}, {-0.40895, 0.16455}, {-0.4089, 0.145141}}

$\endgroup$
2
$\begingroup$
list = {{99., 4.}, {-99., 5.}, {-0.4096, 0.15332}, {-0.40955, 0.13269}};

Using SequenceSplit (new in 11.3)

First @ SequenceSplit[list, {{a_, _} /; ! Between[a, {-0.496, 0.4089}]}]

{{-0.4096, 0.15332}, {-0.40955, 0.13269}}

Using Cases

Cases[ a: {b_, _} /; Between[b, {-0.496, 0.4089}] :> a] @ list

{{-0.4096, 0.15332}, {-0.40955, 0.13269}}

SequenceSplit is currently (V 13.3) slow with long lists.

$\endgroup$
2
$\begingroup$

Using Interval functionality:

Clear["Global`"];
datalist = {{-0.4096, 0.15332}, {-0.40955, 0.13269}, {-0.4095, 
    0.175537}, {-0.40945, 0.130859}, {-0.4094, 0.157714}, {-0.40935, 
    0.121826}, {-0.4093, 0.137207}, {-0.40925, 0.135009}, {-0.4092, 
    0.118774}, {-0.40915, 0.126831}, {-0.4091, 0.127441}, {-0.40905, 
    0.14392}, {-0.409, 0.145507}, {-0.40895, 0.16455}, {-0.4089, 
    0.145141}, {-0.40885, 0.138305}, {-0.4088, 0.141845}, {-0.40875, 
    0.145874}, {-0.4087, 0.125122}, {-0.40865, 0.140747}, {-0.4086, 
    0.124145}, {-0.40855, 0.146362}, {-0.4085, 0.139892}};

int = Interval[{-0.4089, -0.4096}];

Pick[datalist, IntervalMemberQ[int, First@#] & /@ datalist]

Select[IntervalMemberQ[int, First@#] &][datalist]

Result:

{{-0.4096, 0.15332}, {-0.40955, 0.13269}, {-0.4095, 0.175537}, {-0.40945, 0.130859}, {-0.4094, 0.157714}, {-0.40935, 0.121826}, {-0.4093, 0.137207}, {-0.40925, 0.135009}, {-0.4092, 0.118774}, {-0.40915, 0.126831}, {-0.4091, 0.127441}, {-0.40905, 0.14392}, {-0.409, 0.145507}, {-0.40895, 0.16455}, {-0.4089, 0.145141}}

$\endgroup$
2
$\begingroup$
list = {{99., 4.}, {-99., 5.}, {-0.4096, 0.15332}, {-0.40955, 0.13269}};

Grabbing the @eldo's list and using DeleteCases:

DeleteCases[list, {a_, _} /; ! Between[a, {-0.496, 0.4089}]]

(*{{-0.4096, 0.15332}, {-0.40955, 0.13269}}*)

Or using SequenceCases:

SequenceCases[list, {s : {a_, _} /; Between[a, {-0.496, 0.4089}]} :> s]

(*{{-0.4096, 0.15332}, {-0.40955, 0.13269}}*)

Or using Pick and IntervalIntersection:

int = Interval[{-0.496, 0.4089}];

Pick[#, IntervalIntersection[int, Interval @@ #] =!= Interval[] & /@ #] &@list

(*{{-0.4096, 0.15332}, {-0.40955, 0.13269}}*)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.