在2D Numpy数组中查找常见元素 [英] Find common elements in 2D numpy arrays
问题描述
如果我有两个(或更多)2D数组,如何在给定行号的情况下仅获取数组之间的公共元素.例如,我有以下格式的数组:
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
我想让两个数组只具有相同的时间元素,所以第0行.我可以使用
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])
给出了所有公共时间,但是我想从array1/2中提取所有匹配的行/列,或者删除非公共时间元素.到底
array1
和array2
将具有完全相同的尺寸,所以我可以去:
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[:, 2]
数组的大小可以不同,例如:
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]]
我只想提取公共时间元素,因此array1和array2仅在Time = 1和Time = 3时包含,因为它们是公共时间元素.然后我可以去:
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]]
推荐答案
如果您有以下数组:
import numpy as np
array1 = np.array([[1, 100.0, 0.0, 0.0], [2, 110.0, 0.0, 0.0], [3, 120.0, 0.0, 0.0]])
array2 = np.array([[1, 101.0, 0.0, 0.0], [3, 119, 0.0, 0.0]])
正如您所说的,您可以使用np.intersect1d
来获取交集,剩下的唯一事情就是为数组建立索引:
As you said you can use np.intersect1d
to get the intersection, the only thing remaining is to index the arrays:
intersect = np.intersect1d(array1[:, 0], array2[:, 0])
array1_matches = array1[np.any(array1[:, 0] == intersect[:, None], axis=0)]
array2_matches = array2[np.any(array2[:, 0] == intersect[:, None], axis=0)]
然后您可以减去它们:
>>> array1_matches - array2_matches
array([[ 0., -1., 0., 0.],
[ 0., 1., 0., 0.]])
这假设您的时间是唯一且经过排序的.如果它们未排序,您可以在之前对其进行排序:
This assumes that your times are unique and sorted. In case they are unsorted you could sort them before:
>>> array1 = array1[np.argsort(array1[:, 0])]
>>> array2 = array2[np.argsort(array2[:, 0])]
万一时间不唯一,我也不知道你要怎么处理,所以我不能在那儿给你建议.
In case the times are not-unique I have no idea how you want to handle that, so I can't advise you there.
这篇关于在2D Numpy数组中查找常见元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!