I'm trying to find the first occurrence of any row in an array in which either column has a number that has changed since the last time it appeared. Given the array below:
import numpy as np
arr = np.array([[1, 11], [2, 21], [3, 31], [4, 41], [1, 11], [2, 21], [3, 31], [4, 42]])
The output I'm looking for would look like:
subArr = [[1, 11]
[2, 21]
[3, 31]
[4, 41]
[4, 42]]
In the actual problem, the numbers are not as sequential as they appear here and cannot be predicted in advance. I've tried finding the first instance in an array, using multiple conditions, trying to get the first element in a 2-D array, and accessing the ith column. Although some of these were helpful but I can't get it do all the things I want. I tried:
subArr = arr[np.unique(np.logical_and(arr[:,0][0], arr[:,1][0]))]
which didn't work. I also tried:
subArr = arr[(arr[:,0][0]) & (arr[:,1][0])]
I'm sure it's just a matter of getting the syntax right but I can't figure out what I'm missing. Any help would be greatly appreciated.
Using:
Python 3.6
Numpy 1.18.1