The Math Forum

Search All of the Math Forum:

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

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

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

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

Advanced Search

Back to Topic List Back to Topic List  
Tom Zeller

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

>The problem: 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
> v = {3, 82, 7, -12, 5};
> b = {False, True, True, False};
> s[v, b]
>{82, 7}
>Of course, a looping solution is obvious. But I want something that
>works on the entire list at once (as in the APL or J programming
>languages, where this sort of thing is utterly trivial). The best
>I've come up with so far is the following:
> bpick[x_, b_] := If[b, {x}, {}]
> SetAttributes[bpick, Listable]
> s[v_, b_] := Flatten[bpick[v, b]]
>Is there some nicer, more direct way? No Table's, Do's, or For's,

Like the passengers of the Titanic, I will attend to the tip of the iceberg
only ......

Here is a combination of Mathematica list handling functions and a pure
function that will do what you ask above.


v = {3, 82, 7, -12, 5};
b = {False, True, True, False, True};

(* list b was added to to make the lists the same length *)

Here it is in steps:

pairs=Transpose[{v, b}]
{{3, False}, {82, True}, {7, True}, {-12, False}, {5, True}}

Select[pairs, #[[2]]&]

{{82, True}, {7, True}, {5, True}}

Map[First, %]
{82, 7, 5}

Here are the steps rolled into a function.

f[x_List, y_List] := Map[First, Select[Transpose[{x,y}], #[[2]]&]]

f[v, b]
{82, 7, 5}

Tom Zeller

Wait! what is that under the water?!

>Actually, the general problem as I formulated it above is just the tip
>of the iceberg of things that are giving me trouble in Mathematica
>that are so easy for me in APL or J. For example, 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). Here the best I've come up with that
>avoids explicit looping is:
> nonzero[x_] != 0; SetAttributes[nonzero, Listable]
> firstnonzero[v_] := First[s[Range[Length[v]], nonzero[v]]]
>In these and similar problems, I have the feeling that there ought to
>be some slick approach using patterns, but I don't see how to do it.
>Any help in putting me onto the right track in "Mathematica thinking"
>about such problems would be appreciated.
> Murray Eisenberg Internet:
> Mathematics & Statistics Dept. Voice: 413-545-2859 (W)
> University of Massachusetts 413-549-1020 (H)
> Amherst, MA 01003 Fax: 413-545-1801

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

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.