RE: For Routine to Map Routine

• To: mathgroup at smc.vnet.net
• Subject: [mg70445] RE: [mg70400] For Routine to Map Routine
• From: "David Annetts" <davidannetts at aapt.net.au>
• Date: Mon, 16 Oct 2006 02:36:00 -0400 (EDT)

```Hi Benedetto,

> 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];

You are attempting to find all elements in a list such that the first
element in successive list elements is identical.  From this sublist, you
want only the first entry.

Given that I want to test with 100 elements rather than 2185850,

TableForm[ans = Array[Random[Integer, {0, 10}] &, {100, 5}]];
Dimensions@ans

We can isolate the first element from each list using

tst = ans[[All, 1]]

If successive elements are identical, then differences between them will be
zero

Position[tst - RotateLeft@tst, 0]

Documentation for Part[] shows that we can use its output to take specific
elements from an array.  In conjunction with the last step we have

ans[[#]] & /@ Position[tst - RotateLeft@tst, 0]

This has an extra level of nesting that we can remove by mapping Flatten.
Again, in conjunction with the last step ...

Map[Flatten, ans[[#]] & /@ Position[tst - RotateLeft@tst, 0], 1]

You want only the first part of this array.  Putting it all together, we
have

tst = ans[[All, 1]];
tmp = Map[Flatten, ans[[#]] & /@ Position[tst - RotateLeft@tst, 0],
1][[All, 1]]

If you're _really_ not interested in the other elements of arrays, then

Flatten[ans[[All, 1]][[#]] & /@ Position[ans[[All, 1]] -
RotateLeft@ans[[All, 1]], 0]]

Gives the same result.

Regards,

Dave.

--
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.408 / Virus Database: 268.13.4/476 - Release Date: 14/10/2006

```

• Prev by Date: Fwd: Beginner--[Plz Help] I don't want to display a POLARPLOT but use SHOW Command
• Next by Date: Re: Convert expression to polynomial
• Previous by thread: Re: For Routine to Map Routine
• Next by thread: Trace and Wolfram Workbench and Print