Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
Drexel University or The Math Forum.


C. Frey
Posts:
1
Registered:
12/7/04


[mg6133] Re: [mg6120] selecting from lists
Posted:
Feb 23, 1997 12:14 AM


Hi!
I think there a some more MMAlike methods to solve the problems you posted (getting those elements of a list for which the corresponding element of another list is True resp. getting the first element of a list having a special property)
Take the following as an example
IN[1]:=v = {1,2,3,4,5}; b = {True,False,False,True,True};
Then by typing
IN[2]:=Cases[Transpose[{v,b}],{_,True}] OUT[2]={{1, True}, {4, True}, {5, True}}
you get all the pairs from the matrix Transpose[{v,b}] which match the pattern {_,True}. But in Cases you can also specify a Rule which transforms these pairs (because you actually want the numbers, not the pairs). So this is an answer to your first question
IN[3]:=Cases[Transpose[{v,b}],{val_,True}>val] OUT[3]={1,4,5}
Now you also searched for the first element in a list matching a condition or conditional pattern. The following is a solution if you look for the first number in the upper list v which is greater than 2 (e.g.) :
IN[4]:=Cases[v, val_/;val>2] //First OUT[4]=3
It has to be mentioned that in all the above you could also use Select instead of Cases together with boolean functions instead of (conditional) patterns. But when it comes to rules, I think Cases is the better approach...
Clemens (: Clemens.Frey@unibayreuth.de :)



