Wednesday, May 27, 2015

An Example Using Pick to Select Data by Pattern Instead of Cases, Select, or StringCases


Prefatory remark: To learn Mathematica efficiently you should focus on the most-frequently-used functions. I will provide a list of those soon. For most users Pick[] will not be a frequently-used function. You can learn "esoteric" Mathematica functions just for fun or because in your work certain esoteric functions are handy.

Here, nonetheless, is an example spawned from my not being able to get StringCases to work (solved and explained here), altho strangely StringMatchQ does work, which is the standard way to see if StringCases will work.

This is a file header from a parameter sweep I did in the finite element modeling program COMSOL. The goal is to select the different parameters used in the sweep, which are voltages such as "VCathode=2.29025"; I don't need all the other noise in there.

The way the command works is: 1) StringMatchQ is mapped onto data, which is a List of Strings, and returns True if the target matches the String with "*" wildcard, or False otherwise; 2) Pick then scans data to find instances matching the selection pattern. In this case since Pick's default selection pattern is True, I didn't need to use its 3rd argument that specifies a different selection pattern (such as "1" vs. "0").

data = {"%","cln1x","V","(V)","@","VCathode=2.75392","V","(V)","@","VCathode=2.29025","V","(V)","@","VCathode=1.86092","V","(V)","@","VCathode=1.59846","V","(V)","@","VCathode=1.40122","V","(V)","@","VCathode=1.28137","V","(V)","@","VCathode=1.15876","V","(V)","@","VCathode=1.06451","V","(V)","@","VCathode=1.0037","V","(V)","@","VCathode=0.950678","V","(V)","@","VCathode=0.881652","V","(V)","@","VCathode=0.836608","V","(V)","@","VCathode=0.795234","V","(V)","@","VCathode=0.753297","V","(V)","@","VCathode=0.73737","V","(V)","@","VCathode=0.725229","V","(V)","@","VCathode=0.703015","V","(V)","@","VCathode=0.68059","V","(V)","@","VCathode=0.646921","V","(V)","@","VCathode=0.62848"};



My next step will be to use Table and Join to insert the correct parameter in the header of the its matching data file from COMSOL.

Trivia: Pick was originally called BinarySelect and Stephen Wolfram re-named it, as is his want, to a short Anglo-Saxon term (a large metal rod is called a pick, or think of using toothpick to pick up food). I ran into him at a ice cream shop and complimented him on the function and asked if he had re-named it. When he was much younger Stephen ate a lot of chocolate and didn't take care of himself physically. But that has changed, he is careful with his diet and spends a lot of time on the treadmill staying in good physical condition.

1 comment:

  1. `Select[data, StringMatchQ[#, "VCathode*"] &]` or
    `Cases[data, _?(StringMatchQ[#, "VCathode*"] &)]` will work.