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

In summary, the conversation discusses the issue of using the Select function in Mathematica to classify a list of lists into groups and plot them based on a specific column value. The issue of defining a variable before it is evaluated is also brought up. The ultimate goal is to create a more efficient method using Sow and Reap to classify the data points and plot them by group. The conversation also confirms that the programming language being discussed is Mathematica.
  • #1
Swamp Thing
Insights Author
970
670
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
  • #2
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.
 
  • #3
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
  • #4
jedishrfu said:
Ahh, what programming language are we talking here? mathematica? maple? not matlab?
1664602773197.png

😉
 
  • #5
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?
 
  • #6
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.
 

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

What is the purpose of using the "Select" function in scientific research?

The "Select" function is used to filter a list of data based on a specific condition or parameter. This allows scientists to focus on a subset of data that is relevant to their research question or hypothesis.

How do you specify the condition in the "Select" function?

The condition is specified within the square brackets after the list of data. For example, if you want to select only data points that are greater than a certain value, you would use the condition "> value" within the square brackets.

Can multiple conditions be used in the "Select" function?

Yes, multiple conditions can be used by separating them with logical operators such as "AND" or "OR". This allows for more specific filtering of data.

What types of data can be used with the "Select" function?

The "Select" function can be used with any type of data, including numerical, text, or boolean values. It can also be used with more complex data structures such as lists, arrays, or dictionaries.

How is the "Select" function different from other filtering methods in scientific research?

The "Select" function is a more precise and efficient way to filter data compared to other methods such as manual sorting or using conditional statements. It allows for quick and easy filtering of large datasets and provides more flexibility in choosing specific conditions to filter by.

Similar threads

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