Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.math.mathematica

Topic: [mg6133] Re: [mg6120] selecting from lists
Replies: 0  

Advanced Search

Back to Topic List Back to Topic List  
C. Frey

Posts: 1
Registered: 12/7/04
[mg6133] Re: [mg6120] selecting from lists
Posted: Feb 23, 1997 12:14 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

Hi!

I think there a some more MMA-like 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@uni-bayreuth.de :)









Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.