1
$\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$
  • $\begingroup$ try Select[-0.4089<=#[[1]]<=-0.4096&]@datalist $\endgroup$ – kglr May 28 at 8:56
1
$\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!

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thanks!!! Both work exactly as I wanted $\endgroup$ – alpor May 28 at 9:09
0
$\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}}

| improve this answer | |
$\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.