Select[ list, condition] with a parameter in the condition

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
5 replies · 2K views
Swamp Thing
Insights Author
Messages
1,062
Reaction score
819
This works:
Code:
a=0.4
Select[ list,   #[[2]] > a-0.025  && #[[2]] < a+0.025 &   ]

{{401803.,0.42485,3.33299,0.776904,0.277985},{402066.,0.40333,9.23462,0.381478,0.397121},{402872.,0.41899,3.47237,0.742789,0.27385}}

But why doesn't this work? :-
Code:
Select[ list,  #[[2]] > b - 0.025 && #[[2]] < b + 0.025  &    ]  /.  b -> 0.4

{}
 
Physics news on Phys.org
Ahh, what programming language are we talking here? mathematica? maple? not matlab?

my guess is mathematica. am i right? am i right?

off the bat, where is b defined? I don't see it in your listing.

wait okay so b is defined at the end of the second expression so its likely the language has evaluated the select expression without knowing what b is and then its gets defined but you have an empty list.
 
jedishrfu said:
Ahh, what programming language are we talking here? mathematica? maple? not matlab?
1664602773197.png

😉
 
The ultimate goal is to classify a list of lists into groups, based on say the value of column 2. Then I'd like to ListPlot column 1 versus column 4 from each group, each with its own color.

I have done this manually for three groups:

Code:
ListPlot[{
    {#[[1]], #[[4]]} & /@    Select[mxs1, #[[2]] > 0.40 &&  #[[2]] < 0.42 &],
    {#[[1]], #[[4]]} & /@    Select[mxs1, #[[2]] > 0.45 &&  #[[2]] < 0.47 &],
    {#[[1]], #[[4]]} & /@    Select[mxs1, #[[2]] > 0.50 &&  #[[2]] < 0.52 &]
        }, PlotRange -> {0, 5}, ImageSize -> 300,
 PlotStyle -> {Red, Green, Gray}]
1664605722283.png

How can I do this for N groups by iterating through a selection parameter, say {0.4, 0.45, 0.5} in the above example?
 
I just realized that this general approach is inefficient because each data point is examined once for each classification bin.

I will try scanning all the data points just once, while sending each point to a destination sub-group using Sow and Reap. I will post again in case I get stuck.