Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1995
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1995

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

Search the Archive

Re: Re: switch list elements, index from inside ??

  • Subject: [mg2698] Re: [mg2675] Re: [mg2644] switch list elements, index from inside ??
  • From: hay at haystack.demon.co.uk (Allan Hayes)
  • Date: Sat, 9 Dec 1995 01:51:54 -0500
  • Approved: usenet@wri.com
  • Distribution: local
  • Newsgroups: wri.mathgroup
  • Organization: Wolfram Research, Inc.


David Withoff's way ([mg2675],attached) is not only clearer, it is  
also quicker.

Example

t={1,2,3,4,5,6,7,8,9,10,11};

My suggestion
   s[list_List]:=
      Unevaluated[
         ReplacePart[ReplacePart[list,list[[n]],m],list[[m]],n]
      ]//.{m->Random[Integer,{1,10}],n->Random[Integer,{1,10}]}

Dave's suggestion
   s2[list_List]:=
      With[{m = Random[Integer, {1, 10}],
            n = Random[Integer, {1, 10}]},
            ReplacePart[ReplacePart[list,list[[n]],m],list[[m]],n]
      ]

Timings:

   Do[s[t], {500}];//Timing
   Do[s2[t], {500}];//Timing

     {1.83333 Second, Null}
     {1.23333 Second, Null}

Allan Hayes
hay at haystack.demon.co.uk

*******
Begin forwarded message:

>From: David Withoff <withoff at wri.com>
To: mathgroup at smc.vnet.net
>To: mathgroup at smc.vnet.net
>Subject: [mg2675] Re: [mg2644] switch list elements, index from inside ??
> Organization: Wolfram Research, Inc.

In article <49u4td$kgj at dragonfly.wri.com> Allan Hayes <hay@haystack>  
writes:
> davep at newton.umsl.edu
> in [mg2644] switch list elements, index from inside ??
> writes
>
> >I have a problem regarding the interchanging of list elements
> (using >the routine posted by Richard Mercer)
> >
> >   t={1,2,3,4,5,6,7,8,9,10,11};
> >   s[list_List]:=
> >      ReplacePart[ReplacePart[list,list[[n]],m],list[[m]],n]//.     
>
> >         {m->Random[Integer,{1,10}],n->Random[Integer,{1,10}]}
> >   s[t]
> >
> >Mma complains that the part and position specification (that's m  
> and >n) in ReplacePart is neither an integer nor a list of non zero  
> >integers.Why doesn't Mma accept the (Random-) Integers ?
>
> The part ReplacePart[ReplacePart[list,list[[n]],m],list[[m]],n] is  
> evaluated before m and n are replaced by the integers generated.
> You can use Unevaluated to prevent this:
>
>    t={1,2,3,4,5,6,7,8,9,10,11};
>    s[list_List]:=
>       Unevaluated[
>          ReplacePart[ReplacePart[list,list[[n]],m],list[[m]],n]
>       ]//.{m->Random[Integer,{1,10}],n->Random[Integer,{1,10}]}
>
>    s[t]
>
>
>    {6, 2, 3, 4, 5, 1, 7, 8, 9, 10, 11}
>
> Allan Hayes
> hay at haystack.demon.co.uk
>

Actually, this is an application where I'd be inclined to use With

s2[list_List]:= With[{m = Random[Integer, {1, 10}],
                       n = Random[Integer, {1, 10}]},
            ReplacePart[ReplacePart[list,list[[n]],m],list[[m]],n]]

not because the result is any different, but because it makes it
clearer (to me) that you want m and n to be computed before all
of the ReplacePart stuff.

Dave Withoff
Wolfram Research



  • Prev by Date: Re: Comparison of MMA on Various Machines
  • Next by Date: Re: Scientific Web - Speedcomparison Mathematica 2
  • Previous by thread: Re: switch list elements, index from inside ??
  • Next by thread: Re: Re: switch list elements, index from inside ??