pythonnumpynumpy-ndarraymne-python

delete consecutive duplicates in numpy array


I want to delete second row of an event trigger of this numpy list, the event triggers are decoded in the 3. column (starting with: 2,1,3,4,5). How can I delete the consecutive row of the same event?

Input:

[[  108     0     2]
 [  323     0     2]
 [  543     0     1]
 [  758     0     1]
 [  988     0     3]
 [ 1203     0     3]
 [ 1443     0     4]
 [ 1658     0     4]
 [ 1868     0     5]
 [ 2083     0     5]
 [ 2333     0     5]
 [ 2546     0     5]
 [ 2786     0     4]
 [ 3000     0     4]
 [ 3211     0     1]
 [ 3425     0     1]
 [ 3645     0     2]
 [ 3860     0     2]
 [ 4100     0     3]
 [ 4315     0     3]
 [ 4525     0     3]
 [ 4738     0     3]
 [ 4978     0     2]
 [ 5193     0     2]...

Ouput:

[[  108     0     2]
 [  543     0     1]
 [  988     0     3]
 [ 1443     0     4]
 [ 1868     0     5]
 [ 2333     0     5]
 [ 2786     0     4]
 [ 3211     0     1]
 [ 3645     0     2]
 [ 4100     0     3]
 [ 4525     0     3]
 [ 4978     0     2]...

The ouput array length should then be half of the input array.

I would be really thankful for some help, thank you in advance!


Solution

  • input:

    a = np.array([
        [  108,0,2],
        [  323,0,2],
        [  543,0,1],
        [  758,0,1],
        [  988,0,3],
        [ 1203,0,3],
        [ 1443,0,4],
        [ 1658,0,4],
        [ 1868,0,5],
        [ 2083,0,5],
        [ 2333,0,5],
        [ 2546,0,5],
        [ 2786,0,4],
        [ 3000,0,4],
        [ 3211,0,1],
        [ 3425,0,1],
        [ 3645,0,2],
        [ 3860,0,2],
        [ 4100,0,3],
        [ 4315,0,3],
        [ 4525,0,3],
        [ 4738,0,3],
        [ 4978,0,2],
        [ 5193,0,2]
    ])
    

    solution:

    ids = [0,]
    consecutives = 0
    for i in range(1, len(a)):
        
        if a[i,-1]!=a[i-1,-1]:
            consecutives=0
        else:
            consecutives+=1
        
        if consecutives%2==0:
            ids.append(i)
    
        
    

    output a[ids]:

    array([[ 108,    0,    2],
           [ 543,    0,    1],
           [ 988,    0,    3],
           [1443,    0,    4],
           [1868,    0,    5],
           [2333,    0,    5],
           [2786,    0,    4],
           [3211,    0,    1],
           [3645,    0,    2],
           [4100,    0,    3],
           [4525,    0,    3],
           [4978,    0,    2]])
    

    If you need to speed-up the loop you can simply implement it with numba