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

Click For Summary
SUMMARY

The discussion focuses on the use of the Select function in Mathematica for filtering lists based on dynamic conditions. The initial attempt to use a parameter 'b' in the condition fails because 'b' is not defined at the time of evaluation. The user aims to classify a list of lists into groups based on the second column's values and visualize the results using ListPlot. A more efficient approach is suggested, involving the use of Sow and Reap to categorize data points in a single pass.

PREREQUISITES
  • Understanding of Mathematica syntax and functions
  • Familiarity with list manipulation in Mathematica
  • Knowledge of data visualization techniques using ListPlot
  • Basic concepts of functional programming, particularly in Mathematica
NEXT STEPS
  • Explore the use of Sow and Reap in Mathematica for efficient data classification
  • Learn about dynamic variable evaluation in Mathematica to avoid undefined variables
  • Investigate advanced filtering techniques using Select and conditions in Mathematica
  • Study the customization of ListPlot for visualizing grouped data
USEFUL FOR

Data scientists, Mathematica users, and anyone interested in optimizing data classification and visualization techniques in Mathematica.

Swamp Thing
Insights Author
Messages
1,047
Reaction score
780
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   Reactions: 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 ·
Replies
4
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 18 ·
Replies
18
Views
5K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
8
Views
4K
Replies
5
Views
1K