MathGroup Archive 2002

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

Search the Archive

Re: irritating little problem

  • To: mathgroup at smc.vnet.net
  • Subject: [mg32939] Re: [mg32906] irritating little problem
  • From: Chris Johnson <cjohnson at shell.faradic.net>
  • Date: Wed, 20 Feb 2002 01:26:22 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

This may not be too elegent, but it gets the job done.  Actually, it only
catches pairs, so if something appears 3 times you get the 3 permutations
of matches.  A little extra work might catch higher order matches.

matches[list_List] :=
  Union[
    Select[
      Position[
          Map[
            If[First[#] == Last[#], #, Null] &,
            Outer[List, list, list], {2}]
          , x : {_, _}]
        /. {x_, y_} :> {y, x} /; y < x,
      First[#] != Last[#] &]];

In[60]:= list=Table[Random[Integer,{0,9}],{10}]
Out[60]= {4,3,3,3,4,6,1,5,5,2}

In[61]:= matches[list]
Out[61]= {{1,5},{2,3},{2,4},{3,4},{8,9}}

It took me about 10 seconds to do a List of length 1000.  Probably could
be sped up by cleaning up the duplicates of the Outer earlier.

Good luck!

Chris

On Tue, 19 Feb 2002, KIMIC Weijnitz Peter wrote:

> I have a simple vector 
> and I want to find the position of elements that are equal.
> 
> I.e I want to test the vector and find all cases of similar elements.
> 
> Brute force is not what I want, it can be a long vector.
> Best regards
> Petr W
> 



  • Prev by Date: RE: irritating little problem
  • Next by Date: Re: irritating little problem
  • Previous by thread: RE: irritating little problem
  • Next by thread: Re: Irritating Little Problem