Find the set difference between two large arrays (matrices) in Python

前端 未结 3 1832
天命终不由人
天命终不由人 2020-12-06 16:54

I have two large 2-d arrays and I\'d like to find their set difference taking their rows as elements. In Matlab, the code for this would be setdiff(A,B,\'rows\')

3条回答
  •  不知归路
    2020-12-06 17:44

    I'm not sure what you are going for, but this will get you a boolean array of where 2 arrays are not equal, and will be numpy fast:

    
    import numpy as np
    a = np.random.randn(5, 5)
    b = np.random.randn(5, 5)
    a[0,0] = 10.0
    b[0,0] = 10.0 
    a[1,1] = 5.0
    b[1,1] = 5.0
    c = ~(a-b==0)
    print c

    [[False True True True True] [ True False True True True] [ True True True True True] [ True True True True True] [ True True True True True]]

提交回复
热议问题