If I have two (or more) 2D arrays, how can I get only common elements between the arrays given a row number. For example, I have arrays in the format:
time, position, velocity, acceleration
I want to get the two arrays to only have the same time elements, so row 0. I can use
np.intersect1d(array1[:, 0], array2[:, 0])
which gives all the common times, but I want to either extract all matching rows/columns from array1/2 or remove non common time elements. In the end
array1
and array2
will have the exact same dimensions so I could go:
pos_difference = array1[:, 1] - array2[:, 1]
The arrays could be different sizes, so for example:
array1 = [[1, 100.0, 0.0, 0.0], [2, 110.0, 0.0, 0.0], [3, 120.0, 0.0, 0.0]]
array2 = [[1, 101.0, 0.0, 0.0], [3, 119, 0.0, 0.0]]
And I want to extract only common time elements so array1 and array2 will only contain when Time=1, and Time=3, since those are the common time elements. Then I can go:
pos_difference = array1[:, 1] - array2[:, 1]
and this will be the position differences between the two arrays at the same time:
# First row will be when time=1 and second row will be when time=3
pos_difference = [[0, -1, 0.0, 0.0], [0, 1, 0.0, 0.0]]