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: [mg6132] Re: [mg6120] selecting from lists
Replies: 0  

Advanced Search

Back to Topic List Back to Topic List  
Allan Hayes

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

murray@math.umass.edu (Murray Eisenberg)
[mg6120] selecting from lists

writes
(1)
> given an arbitrary list v and a list b of the same length, whose
> entries are all Boolean (True or False), construct a function
> s[v, b] whose result is a list consisting only of those entries

of > v for which the corresponding entries in b are True.
> For example,
> v = {3, 82, 7, -12, 5};
> b = {False, True, True, False};
> s[v, b]
> {82, 7}


(2)
> construct a function firstnonzero that returns the index of the
> first nonzero entry in a list (or, more generally, the first

entry > in a list satisfying a given property).

Murray:
Here are some ideas:
Both functions , s and p, allow the first n qualifying entries to
be chosen. We can easily go further, in particular by allowing s and
p to pass level specifications.

(1)
s[v_,b_,n_:Infinity]:= Cases[Thread[{v,b}], {x_,True}:>x,1,n]

s[v,b]
{82, 7}

s[v,b,1]
{82}
(2)

p[v_, pattern_, n_:Infinity] := Position[v, pattern, 1, n]

p[{1,2,1,2,2,1}, 1]
{{1}, {3}, {6}}

p[{1,2,1,2,2,1}, 1, 2]
{{1}, {3}}

Allan Hayes
hay@haystack.demon.co.uk
http://www.haystack.demon.co.uk









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.