matching elements of numeric arrays

Discussion in 'Python' started by daniel.neilson@gmail.com, Oct 13, 2005.

  1. Guest

    I have two one-dimensional Numeric arrays, and I need to know the
    indices in the second array of elements from the first.

    so if i had:

    a=array([2,4,6])
    b=array([2,3,4,5,6])

    i want a function match that does this:

    >>> match(a,b)

    array([0,2,4])

    i have something that works, but it involves converting things to lists
    and using 'in', and it is rather slow. if someone could point me to a
    better solution, i would appreciate it.
     
    , Oct 13, 2005
    #1
    1. Advertising

  2. Robert Kern Guest

    wrote:
    > I have two one-dimensional Numeric arrays, and I need to know the
    > indices in the second array of elements from the first.
    >
    > so if i had:
    >
    > a=array([2,4,6])
    > b=array([2,3,4,5,6])
    >
    > i want a function match that does this:
    >
    >
    >>>>match(a,b)

    >
    > array([0,2,4])
    >
    > i have something that works, but it involves converting things to lists
    > and using 'in', and it is rather slow. if someone could point me to a
    > better solution, i would appreciate it.


    If b is sorted, then you can use Numeric.searchsorted(b, a).

    Otherwise:

    In [28]: import Numeric as N

    In [29]: a = N.array([2,4,6])

    In [30]: b = N.array([2,3,4,5,6])

    In [31]: match = N.equal.outer(a, b)

    In [32]: idx = N.compress(N.sum(match), N.arange(len(b)))

    In [33]: idx
    Out[33]: array([0, 2, 4])


    --
    Robert Kern


    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter
     
    Robert Kern, Oct 14, 2005
    #2
    1. Advertising

  3. <> wrote in
    news:...
    > I have two one-dimensional Numeric arrays, and I need to know the
    > indices in the second array of elements from the first.
    >
    > so if i had:
    >
    > a=array([2,4,6])
    > b=array([2,3,4,5,6])
    >
    > i want a function match that does this:
    >
    > >>> match(a,b)

    > array([0,2,4])
    >
    > i have something that works, but it involves converting things to lists
    > and using 'in', and it is rather slow. if someone could point me to a
    > better solution, i would appreciate it.


    I have no idea about Numeric array module, so I will use
    the built-in Python one for it (how about the speed if
    compared to Numeric arrays?):

    from array import array
    a = array('I', [2,4,6])
    b = array('I', [2,3,4,5,6])

    def match(a,b):
    retVal = array('I')
    for item in a:
    retVal.append(b.index(item))
    return retVal

    print a
    print b
    print str(match(a,b))

    Outputs:

    array('I', [2L, 4L, 6L])
    array('I', [2L, 3L, 4L, 5L, 6L])
    array('I', [0L, 2L, 4L])

    Claudio
     
    Claudio Grondi, Oct 14, 2005
    #3
  4. Guest

    Thanks a lot. It was this equal.outer idea that I was missing.
     
    , Oct 14, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    982
    X-Centric
    Jun 30, 2005
  2. darrel
    Replies:
    4
    Views:
    869
    darrel
    Jul 19, 2007
  3. jobs

    int to numeric numeric(18,2) ?

    jobs, Jul 21, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    1,020
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    Jul 22, 2007
  4. Philipp
    Replies:
    21
    Views:
    1,189
    Philipp
    Jan 20, 2009
  5. Nick Black

    matching arrays within arrays

    Nick Black, Jan 23, 2007, in forum: Ruby
    Replies:
    3
    Views:
    158
    Nick Black
    Jan 23, 2007
Loading...

Share This Page