MathGroup Archive 1999

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Lists to mask other lists

  • To: mathgroup at smc.vnet.net
  • Subject: [mg19275] Re: Lists to mask other lists
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Thu, 12 Aug 1999 01:24:27 -0400
  • References: <7o5hi2$rkt@smc.vnet.net> <7ooigc$i0f@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Below I have summarised and timed solutions to this problem (including three
new ones).

Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565



PROBLEM
mask:{m1,m2,...} and deal:{d1,d2,...} are lists of lengh 10000; mask is a
list of zeros and ones. We wish to construct a list {r1,r2,...} of length
10000 with ri = If[ mi==0, a, di].


TIMED SOLUTIONS: decreasing speed from top to bottom.

The performance of ReplacePart (next to last) is surprisingly slow, perhaps
others will check it.

mask = Table[Random[Integer], {10000}];
deal = Table[Random[], {10000}];

(temp = deal;
        temp[[Flatten[Position[mask, 0], 1]]] = a; temp); //
    Timing // First (*Allan Hayes*)
(* temp is used to avoid altering the original deal *)
0.22 Second

(controlList = mask /. {1 -> False, 0 -> True};
MapThread[If[#2, a, #1] &, {deal, controlList}]); //
    Timing // First   (*Hartmut Wolf*)

0.5 Second

Transpose[Transpose[{mask, deal }] /. {0, _} -> {0, a}] // Last; //
    Timing // First (*Allan Hayes*)

0.5 Second

rp[0, _] := a
rp[_, x_] := x
MapThread[rp, {mask, deal }]; // Timing // First
  (*Allan Hayes*)

0.55 Second

MapThread[If[#1 == 1, #2, a] &, {mask, deal }]; //
    Timing // First (*David Park, dkeith at hevanet.com,
    DavidBailey, Tobias*)

0.71 Second

If[# == 1, #2, a] & @@@ (Transpose[{mask, deal}]); //
    Timing // First (*Jens - Peer Kuska*)

0.83 Second

If[#[[1]] == 1, #[[2]], a] & /@ (Transpose[{mask, deal}]); // Timing //
First
  (*Allan Hayes*)

1.1 Second

ReplacePart[deal, a, Position[mask, 0]]; // Timing // First (*Ted Ersek*)

69.43 Second

(F[x_] := a;
pos = Position[mask, 0];
MapAt[F, deal, pos]); // Timing // First (*Peltio*)

87.11 Second



  • Prev by Date: Re: Linux sound output
  • Next by Date: Re: Automatic Display in MatrixForm
  • Previous by thread: Re: Lists to mask other lists
  • Next by thread: FW: Re: Lists to mask other lists