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

AI Thread Summary
The discussion focuses on using the Select function in Mathematica to filter lists based on a parameterized condition. The initial example works when using a defined variable 'a', but fails when attempting to use 'b' because 'b' is not defined prior to evaluation, resulting in an empty list. The goal is to classify a list of lists by the second column's values and plot specific columns from each group. A manual method for three groups is presented, but the user recognizes inefficiencies in this approach. The user plans to optimize the process by scanning data points once and categorizing them using Sow and Reap.
Swamp Thing
Insights Author
Messages
1,028
Reaction score
763
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.
 
The /. tells Mathematica to first evaluate the expression to the left generally and then let b be 0.4 in the resulting expression. It does not work because the expression cannot be evaluated for general b.
 
  • Informative
Likes Swamp Thing
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.
 

Similar threads

Replies
4
Views
3K
Replies
6
Views
2K
Replies
1
Views
2K
Replies
2
Views
2K
Replies
3
Views
2K
Replies
18
Views
4K
Replies
2
Views
3K
Back
Top