Re: For Routine to Map Routine

• To: mathgroup at smc.vnet.net
• Subject: [mg70438] Re: For Routine to Map Routine
• From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
• Date: Mon, 16 Oct 2006 02:35:38 -0400 (EDT)
• Organization: The Open University, Milton Keynes, UK
• References: <egse9i\$css\$1@smc.vnet.net>

```Benedetto Bongiorno wrote:
> To All,
>
> Dimensions of ans01 is 2185850 by 5.
> I am looking to convert my For routine to the more efficient Map routine
>
> n=Length[ans01]
> fo={};
>
> For[i=1, i<n, i++,
> If[ans01[[i,1]] == ans01[[i+1,1]],
> AppendTo[fo, ans01[[1]]]];
>
> I tried
> Map[If[#[[1]] == #[[1]],#]&,ans01];
>
> But it does not work.
>
> Thank you
>
> Ben
>
>
Hi Ben,

The following function is linear in time and is roughly 40 times faster
than your procedure when processing a list of 100,000 elements. 2185850
elements of 5 integers are processed in less than 40 seconds on my system.

In[1]:=
myConvert[data_?MatrixQ]:=
Module[{sp},
sp=Split[data, #1[[1]]\[Equal]#2[[1]]&];
Flatten[Most/@Extract[sp, Position[Length/@sp, x_/;x>1]],1]
]

In[2]:=
ans01=Table[Random[Integer,{0,5}],{2185850},{5}];

In[3]:=
Timing[myConvert[ans01];][[1]]

Out[3]=
37.032 Second

In[4]:=
ans01=Table[Random[Integer,{0,5}],{100000},{5}];

In[5]:=
t1=Timing[n=Length[ans01];
fo={};
For[i=1,i<n,i++,If[ans01[[i,1]]\[Equal]
ans01[[i+1,1]],AppendTo[fo,ans01[[i]]]];]][[1]]

Out[5]=
65.922 Second

In[6]:=
t2=Timing[fo2=myConvert[ans01];][[1]]

Out[6]=
1.703 Second

In[7]:=
fo\[Equal]fo2

Out[7]=
True

In[8]:=
t1/t2

Out[8]=
38.7093

Regards,
Jean-Marc

```

• Prev by Date: Re: General--Simple Permutations
• Next by Date: Re: How to extract numbers from list elements with text?
• Previous by thread: Re: For Routine to Map Routine
• Next by thread: Re: For Routine to Map Routine