Looking for the fastest way to find the exact overlap between two arrays of equal length in numpy

前端 未结 3 982
一生所求
一生所求 2021-01-14 02:58

I am looking for the optimal (fastest) way to find the exact overlap between two arrays in numpy. Given two arrays x and y

x = array([1,0,3,0,5,0,7,4],dtype=         


        
3条回答
  •  旧巷少年郎
    2021-01-14 03:15

    result = numpy.where(x == y, x, 0)
    

    Have a look at numpy.where documentation for explanation. Basically, numpy.where(a, b, c), for a condition a returns an array of shape a, and with values from b or c, depending upon whether the corresponding element of a is true or not. b or c can be scalars.

    By the way, x & y is not necessarily "always true" for two positive numbers. It does bitwise-and for elements in x and y:

    x = numpy.array([2**p for p in xrange(10)])
    # x is [  1   2   4   8  16  32  64 128 256 512]
    y = x - 1
    # y is [  0   1   3   7  15  31  63 127 255 511]
    x & y
    # result: [0 0 0 0 0 0 0 0 0 0]
    

    This is because the bitwise representation of each element in x is of the form 1 followed by n zeros, and the corresponding element in y is n 1s. In general, for two non-zero numbers a and b, a & b may equal zero, or non-zero but not necessarily equal to either a or b.

提交回复
热议问题