2
$\begingroup$

I have a huge list that looks like this:

list = {8, 12, 201, 0.001, 3, 7, 100 
  (InterpolatingFunction[{{0., 0.0018}},"<>"][2916/35] + 
   InterpolatingFunction[{{0., 0.0018}}, "<>"][2916/35]), 
  23, 44, 11, -0.002}

I want to extract only the numbers, specifically the numbers around 10. I tried using this Select[list, _?(7 <= # <= 13 &)] but it returns an empty list. I think this happens because the list has both numbers and the 100 (InterpolatingFunction... that I want to ignore. I would also be happy just to extract ALL numbers hence ignoring the 100(InterpolatingFunction... elements.

Could you give me some tip please?

$\endgroup$
6
  • 1
    $\begingroup$ You can use Select[..., NumberQ[#] && 7 <= # <= 13 & ] $\endgroup$ Mar 13, 2013 at 9:54
  • $\begingroup$ Thank you so much! That makes it! Should I delete the question? $\endgroup$
    – Sos
    Mar 13, 2013 at 10:00
  • 1
    $\begingroup$ You should never delete a question because you got an answer (only if your question was over-the-top stupid). It may be useful to others as well. $\endgroup$ Mar 13, 2013 at 10:13
  • $\begingroup$ Why is that InterpolatingFunctionthere in the first place? Wouldn't you like to have that evaluated to a real number? $\endgroup$ Mar 13, 2013 at 10:15
  • $\begingroup$ @Sjoerd: list comes as result of integrating a system using NDSolve considering around 4000 combinations of parameters. Some of those combinations do not yield a solution for the integration.. If possible, yes, I would like to have it evaluated to a real number, but the numerical integration doesn't finish until the endtime I allocated, and the endtime of the InterpolatingFunction is far from the desired (e.g. 0.018429 out of the desired 83.3). $\endgroup$
    – Sos
    Mar 13, 2013 at 10:58

1 Answer 1

7
$\begingroup$

Select wants a function as second argument, not a pattern. You can either use Cases or Select with a pure function:

Cases[list, _?(7 <= # <= 13 &)]

Select[list, (7 <= # <= 13)&]
$\endgroup$
4
  • $\begingroup$ One more thing: if I want to select the elements of another list otherlist={{1/10,1,1/100,10},{1/100,1,1/100,10},...} that were used as arguments to obtain list={8,12,201,0.001,3,7,100(InterpolatingFunction[{{0.,0.0018}},"<>"][2916/35]+InterpolatingFunction[{{0.,0.0018}},"<>"][2916/35]),23,44,11,-0.002} I thought I could use Pick[otherlist,list,NumberQ[#]&&7<=#<=13&]. Yet, it isn't working and returns that both lists have incompatible shapes. But both lists have the same length.. $\endgroup$
    – Sos
    Mar 13, 2013 at 10:09
  • 1
    $\begingroup$ @Sosi: same problem the other way round: Pick wants, as Cases, a pattern, not a function. Pick[otherlist,list,_?(NumberQ[#]&&7<=#<=13&)] should work, if both lists really have the same structure. Something that might be a problem is that Pick applies the check to all levels of list, so depending on what exactly is the content you might see the shapes-warning. It then probably is better to create a "selection" list independently: Pick[otherlist, (NumberQ[#] && 7 <= # <= 13) & /@ list] $\endgroup$ Mar 13, 2013 at 10:22
  • $\begingroup$ I had tried your first suggestion before, but was getting that structure error. The second suggestion works perfectly, I had not thought about using a map! Thanks! $\endgroup$
    – Sos
    Mar 13, 2013 at 10:31
  • $\begingroup$ @Sosi: here is another question which has more information about what I think is the deeper reason for the "incompatible shapes" warning, in case you are interested... $\endgroup$ Mar 13, 2013 at 17:50

Your Answer

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

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