2

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!

Reby
  • 23
  • 4

1 Answers1

1

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

  • 1
    Thank you for your answer! Unfortunately this method deletes more triggers than only the second same occuring trigger. The 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], .. ]) ouput: a = np.array([ [ 108,0,2], [ 543,0,1],, [ 988,0,3],, .. ]) – Reby Oct 18 '22 at 11:07